Powershell

I’ve recently started to sync my Powershell profile between my work and home PCs.  What I quickly found out however, was that I had some Powershell snapins installed on one computer, but not the other.  Here’s a bit of script which I came up with to handle things cleanly. 

   1: $snapins = "psmsi", # Windows Installer PowerShell Extensions
   2:   "PshX-SAPIEN", # AD cmdlets from Sapien
   3:   "GetGPObjectPSSnapIn", # GPO management
   4:   "PowerPad", # mini-editor
   5:   "Quest.ActiveRoles.ADManagement", # more AD stuff
   6:   "Microsoft.Office.OneNote"
   7: $snapins | ForEach-Object { 
   8:   if ( Get-PSSnapin -Registered $_ -ErrorAction SilentlyContinue ) {
   9:     Add-PSSnapin $_
  10:   }
  11: }

In lines 1-6 you see I’m building an array called $snapins.  Each one of these is the exact name of the snapin as seen when you run ‘get-pssnapin -r’ at the prompt.

Line 7: I use a foreach-object loop to iterate through each item in the array.

Line 8: Get-PsSnapin takes as an optional parameter the name of a snapin, so this If block evaluates to true if the current snapin in the loop (that’s using the $_ automatic variable) is indeed installed.

Line 9: If it’s true…I add the snapin to the current session using the Add-PsSnapin cmdlet.

Hope you find this useful!

: http://halr9000.com/article/425

2007-09-02 21:30:52

[...] Powershell Snapins in your Profile Loading Powershell Snapins in your Profile [link] I’ve recently started to sync my Powershell profile between my work and home PCs. What I [...]

  • Microblog

  • Recent Posts

  • Recent Comments

  • meta

  • PowerShell Blogroll