Prof. Powershell

Snapin Modules

Powershell 2.0 takes snapins a bit farther, adding the 'module' concept.

In PowerShell 1.0, you could add functionality by installing PSSnapins. A PSSnapin is a binary file, usually a DLL, developed by a third-party vendor (sometimes Microsoft) that contains new cmdlets that you can use within a PowerShell session, script or function. Use the Get-PSSnapin command to view all of the currently loaded snapins:

PS C:\> Get-PSSnapin

However this will only show snapins that have been loaded using Add-PSSnapin. To see other snapins registered with PowerShell, use the -Registered parameter:

PS C:\> Get-PSSnapin -registered

PowerShell 2.0 can continue to use PSSnapins but now offers a new concept for extending functionality called a module. A module can be a set of script files or a binary file like a dll. Modules offer a number of benefits and are easier to use. The Get-Module cmdlet displays all currently loaded modules:

PS C:\> Get-Module

Like snapins, you may have modules installed but not loaded. Use the -ListAvailable parameter to display available modules:

PS C:\> Get-Module -ListAvailable

When you are ready to use a module, use the Import-Module cmdlet:

PS C:\> Import-Module ActiveDirectory

Now if you run Get-Module you'll see the new module and its commands. So if conceptually a snapin and a module are the same, can we use the same command? Sure. Just about any snapin can be imported as a module. To import the snapin, you'll need to get the full path to the binary file:

PS C:\> Import-Module (get-pssnapin Quest.ActiveRoles.ADManagement -Registered).ModuleName

On my computer I have the free Quest Active Directory PSSnapin installed. It is not loaded in my current PowerShell session, so I need to use the -Registered parameter with Get-PSSnapin to retrieve information about. What I need is the ModuleName that returns the full path to the DLL:

C:\Program Files\Quest Software\Management Shell for AD\Quest.ActiveRoles.ArsPowerShellSnapIn.dll

To import this as module I simply need to pass it as a value for Import-Module. Even though the Quest tool was packaged and intended to be used as a snapin it won't show when I call Get-PSSnapin. That's because I imported it as a module:

PS C:\> get-module
ModuleType Name                      ExportedCommands
---------- ----                      ----------------
Binary     Quest.ActiveRoles.ArsP... {Get-QADComputer, New-QADUser,...)

Functionally, there's no difference with one exception: When you load a PSSnapin, there is no way to unload it. It remains until you end the PowerShell session. But modules can be unloaded:

PS C:\> Remove-Module Quest*

I only have one Quest "module," so I'll take a shortcut and use a wild card to save some typing.

Modules appear to be the preferred method going forward for adding functionality to your shell, and I'm sure I'll be writing more about them in the future. Snapins probably won't go away anytime soon, but you might start using Import-Module instead of Add-PSSnapin.

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