What's the difference between Add-PsSnapIn and Import-Module
Asked Answered
M

5

37

Powershell has two means for importing additional cmdlets into a session, Add-PsSnapIn and Import-Module. The docs are not at all clear regarding when one would chose one over the other. Does anybody know the difference between these?

Meredeth answered 18/2, 2011 at 7:59 Comment(1)
I found out that the IIS 7 management snapin is now a module as of IIS 7.5 import-module webadministration source: powershell.com/cs/forums/t/8209.aspxEponymy
R
36

Modules came about in PowerShell V2. V2 can still load Snapins though, if necessary. The big difference is that modules can be deployed using Xcopy. There is no need to register anything. To deploy a Snapin, an installer would have to hack the registry, which would require elevated priveleges. To load a module, you simply have to use the Import-Module command.

Modules can have a lot more metadata using a Module Manifest as well, which can specify all kinds of things that might be useful for the end user to know, including version, dependencies on other modules, and which powershell hosts can run the module.

All that being said, if you are writing a binary module in C# or VB, you still use the same PSSnapin base class.

To get more information, check out

PS > help about_modules
Rima answered 18/2, 2011 at 15:20 Comment(1)
"Snapin installer would have to hack the registry" does that mean you can trust snapins more than modules? Are snapins verified by Microsoft? Just curiousDeragon
G
18

In v2, modules are the preferred way organize the cmdlets, providers, functions, aliases, and other commands that you create. You don't install a module. You simply import a module into the PowerShell session using Import-Module cmdlet.

AFAIK, a PowerShell snapin is more of a v1 approach. There are still a few teams at MS creating snapins instead of modules. For example, SharePoint 2010 cmdlets. PowerShell snapins are binaries (.dll) that implement cmdlets and providers. You need to install a snapin and then add the cmdlets in the snapin to a PowerShell session using Add-PSSnapin cmdlet.

Greysun answered 18/2, 2011 at 12:50 Comment(3)
The TFS Power Tools utilities for PowerShell are also still implemented as a snap-in.Bobsleigh
When you say "you don't install a module" you're forgetting that the module must first be xcopy-deployed to one of the Modules folders either personal for current user under %USERPROFILE%\Documents, or machine's global under %WINDIR%\system32\WindowsPowerShell\v1.0\Modules.Dinky
That is partially correct. Using Import-Module, you can specify the absolute path to a module file and load it.Greysun
C
4

This is poorly documented, so take my answer with a grain of salt. Take a look at developer documents for snapin and modules. Briefly, snapin is "just" a .Net assebly whilst module can contain scripts, asseblies and more.

Card answered 18/2, 2011 at 10:42 Comment(0)
T
1

PSSnapin provides a way to protect your assemblies by installing DLLs in protected directory as compared to Module which can be played by just replacing files.

Ref: MSDN Link for PSSnapin

Transit answered 17/2, 2016 at 18:0 Comment(0)
S
1

Add-PSSnapin and Import-Module are used to take external 3rd party libraries(Script files/ binary files/ dll) in the current PowerShell session. Modules are little more easier to use than PSSnapins.

The main advantage module over PSSnapin is we can't remove or unload PSSnapin from the current PowerShell session once it is added. But modules can be removed from the current PowerShell session manually, using Remove-Module

NOTE: The concept PSSnapin introduced in PS version 1.0 and module introduced in PS version 2.0.

Ref:- This

Saddlebag answered 2/5, 2020 at 2:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.