Using Snap-Ins
Snap-ins are used to show a list
of all the registered PSSnapins outside of the default snap-ins that
come with PowerShell. Entering the command Get-PSSnapin -Registered on a newly installed PowerShell system will return nothing, as shown in the following example:
PS C:\> get-pssnapin -registered
In most cases, a setup
program will accompany a PowerShell snap-in and ensure that it becomes
correctly registered for use. However, if this is not the case, the .NET
utility InstallUtil.exe is used to complete the registration process. In the following example, InstallUtil.exe is being used to install a third-party library file called freshtastic-automation.dll:
PS C:\> & "$env:windir\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe" freshtastic-automation.dll
Once the DLL library file
has been registered with PowerShell, the next step is to register the
DLL’s snap-in with PowerShell so that the cmdlets contained in the DLL
are made available to PowerShell. In the case of the
freshtastic-automation library, the snap-in is registered by using the
command Add-PSSnapin freshtastic, as follows:
PS C:\> add-pssnapin freshtastic
Now that the freshtastic snap-in has been registered, you can enter the following command Get-Help freshtastic to review the usage information for the freshtastic cmdlets:
PS C:\> get-help freshtastic
Now that the registration of the
freshtastic library DLL is complete and the associated snap-in has been
added to the console, you can enter the command Get-PSSnapin –registered again and see that the freshtastic snap-in has been added to the console:
PS C:\> get-pssnapin -registered
Name : freshtastic
PSVersion : 2.0
Description : Used to automate freshness.
PS C:\>
Now
that you have registered the third-party library file and added its
snap-in to the console, you might find that the library does not meet
your needs, and you want to remove it. The removal process is basically a
reversal of the installation steps listed previously. First, you remove
the snap-in from the console using the command Remove-PSSnapin freshtastic, as follows:
PS C:\> Remove-PSSnapin freshtastic
Once the third-party snap-in has been unregistered, you will once again use InstallUtil.exe with a /U switch to unregister the DLL, as follows:
PS C:\> & "$env:windir\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe" /U
freshtastic-automation.dll
Once the uninstall has completed, you can verify that the library file was successfully unregistered by entering the command Get-PSSnapin -registered and verifying that no third-party libraries are listed.
Using Modules
In Windows Server 2008 R2, a set
of base modules are loaded when the operating system is installed.
Additionally, modules can be added or removed using the Add Features
Wizard in Server Manager.
Default Module Locations
There are two default locations for modules. The first location is for the machine, as follows:
$pshome\Modules (C:\Windows\system32\WindowsPowerShell\v1.0\Modules)
The second location is for the current user:
$home\Documents\WindowsPowerShell\Modules
(UserProfile%\Documents\WindowsPowerShell\Modules)
Installing New Modules
As mentioned previously, new
modules can be added using the Add Features Wizard in Server Manager.
Additionally, other modules should come with an installation program
that will install the module for you. However, if need be, you can also
manually install a new module. To do this, use the following steps:
1. | Create a new folder for the module that is being installed. For example:
PS C:\> New-Item -type directory -path $home\Documents\WindowsPowerShell\Modules\Spammer1000
|
2. | Copy the contents of the module into the newly created folder.
|
Using Installed Modules
After a module has been
installed on a machine, it can then be imported into a PowerShell
session for usage. To find out what modules are available for use, use
the Get-Module cmdlet:
PS C:\> Get-Module -listAvailable
Or, to list modules that
have already been imported into the current PowerShell session, just use
the Get-Module cmdlet without the listAvailable switch parameter:
Next, to import a module
into a PowerShell session, use the Import-Module cmdlet. For example, if
the ActiveDirectory module has been installed, the following command
would be used:
PS C:\> Import-Module ActiveDirectory
Note
A complete path to the
module folder must be provided for modules that are not located in one
of the default modules locations or any additional module locations that
have been defined for the current PowerShell session. This is required
when using the Import-Module cmdlet to define the module location used
by the cmdlet.
Additionally, if you want to
import all modules that are available on a machine into a PowerShell
session, one of two methods can be used. The first method is to execute
the following command, which lists all modules and then pipes that to
the Import-Module cmdlet:
PS C:\> Get-Module -listAvailable | Import-Module
The second method is to
right-click the Windows PowerShell icon in the taskbar, and then select
Import System Modules. Additionally, you can also use the Windows
PowerShell Modules shortcut, which is found in Control Panel, System and
Security, Administrative Tools.
Note
By
default, modules are not loaded into any PowerShell session. To load
modules by default, the Import-Module cmdlet should be used in
conjunction with a PowerShell profile configuration script.
Removing a Module
The act of removing a module
causes all the commands added by a module to be deleted from the
current PowerShell session. When a module is removed, the operation only
reverses the Import-Module cmdlet’s actions and does not uninstall the
module from a machine. To remove a module, use the Remove-Module cmdlet,
as shown here:
PS C:\> Remove-Module ActiveDirectory