08 March 2013 CI Team

Powershell offers to ways to expand CmdLet’s or Providers: SnapIns and Modules. But what is the difference?

TL;DR:

Snapins Modules
Model from PS 1.0 Variante – “old” Introduced with PS 2.0
Defined in Assemblies Defined in Assemblies or in Scripts
Complex deployment via Installutil Moduls in the file system

 

Snapins: the old way

Snapins (like for example WebAdministration PSSnapin in the era of Windows Server 2008) are the old way which could be used in the first version of Powershell.

Disadvantages of this method:

- The PSSnapins have to be written in a .NET language and have to be available as an Assembly (there are some Sample Codes for the production of PSSnapIns in MSDN)

- The Assemblies have to be installed with a installutil.exe

 

What PSSnapins are installed on my system?

You will find all installed Snapins on the CmdLet “Get-PSSnapin-registered”:

image1766

 

Where are the PSSnapins saved? Of course in the registry!

The registration of the PSSnapins happens in the Registry:

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns\

image

 

What CmdLets are responsible for the administration of the PSSnapins?

Get-PSSnapIn –registered: delivers all available PSSnapIns
Add-PSSnapIn [name]: adds the PSSnapIn to the PowerShell Session
Get-Command –pssnapin [name]: adds CmdLets to the PSSnapin (has to be added via add to the session in the beginning)

All in all you should not create Snapins anymore – but they are still in use and therefore this explanation.

 

PowerShell modules – the new world

The modules get established with the second version of PowerShell and they offer a lot more opportunities without the need for registration in the registry.

Where are the modules saved?

If the only task is to import a module via the module-name with Import-Module usually it takes a look into the folders which are defined in the environment variable $env:psmodulepath:

image

Otherwise it is also possible to define a certain pad to Powershell modules at the Import-Module.

Note: In Powershell 3.0 all available modules are activated automatically in the session and you don’t have to load them with the Import-Module.

 

What modules are “installed”?

You will find a list on “Get-Module –ListAvailable”:

image

 

What is the “ModuleType”?

Other then PSSnapins modules can consist of simple scripts, a workflow, an assembly or a manifest. Therefore is the definition of the ModuleType:

Binary
A module whose members are defined within an assembly (
.dll extension), such as a snap-in or a class library that contains cmdlet classes. This field is introduced in Windows PowerShell 2.0.

Cim
A cmdlets-over-objects module (
.cdxml extension).

Manifest
A module that is defined by a module manifest file (
.psd1 extension) whose ModulesToProcesskey is empty. This field is introduced in Windows PowerShell 2.0.

Script
A module whose members are defined within a script module file (
.psm1 extension). This field is introduced in Windows PowerShell 2.0.

Workflow
A workflow module (
.xaml extension).

From this MSDN article.

 

What does such a module look like? Let’s have a look at the “WebAdministration” module

The “WebAdministration”-Module (to administrate the IIS) is located in the usual “WindowsPowershell” folder and it contains some files:

image

The .psd1 file is the Module Manifest and in fact what is important:

@{
GUID='{13c15630-959c-49e4-a977-758c5cc93408}'
Author='Microsoft Corporation'
CompanyName='Microsoft Corporation'
Copyright='© Microsoft Corporation. All rights reserved.'
ModuleVersion='1.0.0.0'
PowerShellVersion='3.0'
CLRVersion='4.0'
TypesToProcess='iisprovider.types.ps1xml'
FormatsToProcess='iisprovider.format.ps1xml'
NestedModules='Microsoft.IIS.PowerShell.Provider.dll'
RequiredAssemblies='Microsoft.IIS.PowerShell.Framework.dll'
AliasesToExport='Begin-WebCommitDelay','End-WebCommitDelay'
HelpInfoUri="http://go.microsoft.com/fwlink/?LinkID=216903"
}

The main functionality of the Powershell module is in the Microsoft.IIS.PowerShell.Provider.dll CLR 4.0 GAC.

 

How do I write a Windows Powershell Module?

After all the development of .NET based Powershell Modules is quite similar to the old Snapins:

How to Create a Powershell 2.0 Module and Cmdlet with Visual Studio 2010 (Screencast included)

A good reference is also Windows Azure CmdLets.

 

How do I register new modules?

The CmdLets for the registration of modules is also similar to the Snapins:

Get-Module -ListAvailable
Import-Module name
Get-Command -module name