Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Server

Windows Server 2008 R2 : Using Windows PowerShell (part 4) - Using Snap-Ins & Using Modules

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
3/15/2011 11:41:19 AM

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:

PS C:\> Get-Module

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
Other -----------------
- Windows Server 2008 R2 : Automating Tasks Using PowerShell Scripting - Introduction to PowerShell
- Windows Server 2008 R2 : Automating Tasks Using PowerShell Scripting - Understanding Shells
- Windows Server 2003 : Using DNS Monitoring Tools (part 2) - Monitoring DNS Performance with System Monitor
- Windows Server 2003 : Using DNS Monitoring Tools (part 1) - Using Replication Monitor
- Windows Server 2003 : Using DNS Troubleshooting Tools (part 3) - DNS Debug Log
- Windows Server 2003 : Using DNS Troubleshooting Tools (part 2) - Viewing the DNS Events Log
- Windows Server 2003 : Using DNS Troubleshooting Tools (part 1) - Querying DNS with Nslookup
- Maintaining Windows Server 2008 R2
- Windows Server 2008 R2 : Keeping Up with Service Packs and Updates
- Managing Windows Server 2008 R2 Remotely
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server