Prof. Powershell

Snappy SnapIns

Plus, the magical Get-Command cmdlet that allows you to be all-knowing.

PowerShell 1.0 stores all of its cmdlets in snapins. Microsoft ships several that are automatically loaded when you start PowerShell and you can install additional ones from companies like PowerGadgets and Quest Software or obtain free ones from the PowerShell Community Extensions. To see what snapins are currently installed in your system use, this command:

PS C:\> get-pssnapin

You should see a list and description of all snapins. Now it's possible to have a snapin installed but not loaded into your PowerShell session. Use this command to see all registered snapins:

PS C:\> get-pssnapin -registered

What you'll see is a list of all the non-Microsoft snapins that you've installed. To load a snapin, run this:

PS C:\> add-pssnapin snapin-name

Now that you have a snapin, what's inside it? All you have to do is ask using the Get-Command cmdlet:

PS C:\> get-command -pssnapin pscx

In my case, I'll see all the cmdlets that ship with the PowerShell Community Extensions. The snapin must be loaded into your PowerShell session. But what about all the snapins? This one-liner will look at every snapin currently running and display all the cmdlets within each snapin. The results are formatted as a table, grouped by snapin.

PS C:\> get-pssnapin | foreach {get-command -pssnapin $_.name | format-table -groupby pssnapin name,definition}

This expression tells PowerShell, "for every pssnapin name, call Get-Command -pssnapin and then pipe the results to format-table grouping them by the snapin name and display the name and definition properties." You could take this a step further and save the output to a text file this way:

PS C:\> get-pssnapin | foreach {get-command -pssnapin $_.name | format-table -groupby pssnapin name,definition}

Before we wrap up, let's look at one more approach. Say you want to find all the cmdlets that have "file" in the name. You can use a variation of the preceding expression and filter it using the Where cmdlet:

PS C:\> get-pssnapin | foreach {get-command -pssnapin $_.name | where {$_.name -like "*file*"} | select PSSnapin,name,definition}

PSSnapIn                     Name                Definition
--------                     ----                ----------
Microsoft.PowerShell.Utility Out-File            Out-File [-FilePath]...
PSCX                         Get-FileVersionInfo Get-FileVersionInfo ...
PSCX                         Set-FileTime        Set-FileTime [-Path]...

I've selected the Pssnapin property in addition to the Name and Definition, so I can tell where a particular cmdlet has come from. In all examples, Get-Command is the magic snapin opener. I encourage you to take a look at the cmdlet's help to discover more ways to use it.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular