PowerShell snap-ins (PSSnapin) were the 1.0 way of extending PowerShell’s functionality.
In this post of the series “PowerShell 1.0 – The First Cmdlets“, we will cover Get-PSSnapin , Add-PSSnapin , and Remove-PSSnapin , which encompass the cmdlets needed for managing PowerShell snap-ins.
Snap-ins vs Modules: what’s the difference?
In PowerShell 1.0, there was no support for modules, so the only way to extend PowerShell’s functionality was through snap-ins, hence the PSSnapin cmdlets. Due to shortcomings that arose with snap-ins, modules were introduced in PowerShell 2.0 and became the preferred way to add features and functionality.
Kyle Ruddy from VMware did a good job of summing up the differences and advantages of modules in his 2016 post, Saying Farewell to Snapins. Here is a summary of his breakdown:
PowerShell Snap-ins
- Can only be written in a .NET programming language.
- Have to be assembled before being handed off to the user.
- Snap-ins having to be installed and registered to each individual system a user wishes to use them on.
- Snap-ins lack the ability to explicitly define dependencies.
PowerShell Modules
- Modules can be written in any .NET programming language or PowerShell.
- Modules are portable and don’t require any registrations. They can be referenced automatically by placing them in one of the proper folders or by specifying the directory path to where they reside for each system requiring access.
In addition, the most recent releases of PowerShell now support easily finding and installing modules from the PowerShell Gallery, making it extremely easy to find and add functionality. Modules are definitely the way to go if you are starting a new PowerShell project.
So why learn the PSSnapin cmdlets?
Though modules are the preferred way to extend PowerShell functionality, there are still snap-in based tools out there that haven’t made the conversion to modules. These cmdlets will help you identify and manage them.
Get-PSSnapin: What is it for and how to use it
Get-PSSnapin will list the snap-ins loaded in the session. If I run it on a vanilla Windows 10 VM, a single snap-in is listed, Microsoft.PowerShell.Core. That is the last snap-in left from PowerShell 1.0; the other original snap-ins have been converted to modules.
PS C:\Windows\system32> Get-PSSnapin Name : Microsoft.PowerShell.Core PSVersion : 5.1.16299.64 Description : This Windows PowerShell snap-in contains cmdlets used to manage components of Windows PowerShell.
Get snap-ins registered on the computer
If you want to list the snap-ins that have been registered on a computer, run Get-PSSnapin -Registered . The snap-ins don’t have to be loaded in the current session to be listed, however the Core snap-in is not listed because it was installed with Windows PowerShell. When I run it on my vanilla Windows 10 VM, nothing is returned:
PS C:\Windows\system32> Get-PSSnapin -Registered PS C:\Windows\system32>
Add-PSSnapin: What it is for and how to use it
Add-PSSnapin loads a registered snap-in into the current PowerShell session. The keyword is registered, meaning that it has been installed properly on the computer.
For demo purposes, I am going to install the Solarwinds Orion SDK on my Windows 10 VM. The SDK includes a PowerShell snap-in. After running the MSI installer, I can now run Get-PSSnapin -Registered and you can see we now have a snap-in listed:
PS C:\Windows\system32> Get-PSSnapin -Registered Name : SwisSnapIn PSVersion : 5.1 Description : PowerShell Snap-in for the SolarWinds Information Service
If I open a new session and run Get-PSSnapin, only the Core snap-in will be listed. To add the Solarwinds snap-in to the session so I can use it, I run Add-PSSnapin -Name SwisSnapIn , and then run Get-PSSnapin again and now the Solarwinds snap-in is listed as loaded for my current session.
PS C:\Windows\system32> Get-PSSnapin Name : Microsoft.PowerShell.Core PSVersion : 5.1.16299.64 Description : This Windows PowerShell snap-in contains cmdlets used to manage components of Windows PowerShell. PS C:\Windows\system32> Add-PSSnapin -Name SwisSnapIn PS C:\Windows\system32> Get-PSSnapin Name : Microsoft.PowerShell.Core PSVersion : 5.1.16299.64 Description : This Windows PowerShell snap-in contains cmdlets used to manage components of Windows PowerShell. Name : SwisSnapIn PSVersion : 5.1 Description : PowerShell Snap-in for the SolarWinds Information Service
Remove-PSSnapin: What it is for and how to use it
As you probably guessed, Remove-PSSnapin can be used to unload a snap-in from the current session. I haven’t personally encountered a need to use this, but sticking with our Solarwinds SDK example, you can remove the Solarwinds snap-in by running Remove-PSSnapin -Name SwisSnapIn .
PS C:\Windows\system32> Get-PSSnapin Name : Microsoft.PowerShell.Core PSVersion : 5.1.16299.64 Description : This Windows PowerShell snap-in contains cmdlets used to manage components of Windows PowerShell. Name : SwisSnapIn PSVersion : 5.1 Description : PowerShell Snap-in for the SolarWinds Information Service PS C:\Windows\system32> Remove-PSSnapin -Name SwisSnapIn PS C:\Windows\system32> Get-PSSnapin Name : Microsoft.PowerShell.Core PSVersion : 5.1.16299.64 Description : This Windows PowerShell snap-in contains cmdlets used to manage components of Windows PowerShell.
Next Up: Get-Command
In the next post of the series, we will explore the cmdlet that let’s you explore other cmdlets, Get-Command .
Reference
- Get-PSSnapin | docs.microsoft.com
- Add-PSSnapin | docs.microsoft.com
- Remove-PSSnapin | docs.microsoft.com
Leave a Reply