Posted on

Taking on PowerShell one cmdlet at a time

Share this post: This is part of an ongoing blog series by Adam Gordon. Adam will walk through each PowerShell command every week, explaining when and how to use them. Adam will be covering Get-Module this week.

When should you use the Get-Module
The Get-Module cmdlet retrieves the PowerShell modules that were imported or can be imported into a PowerShell session. Get-Module returns valuable information about each module in the module object it returns. You can pipe the module objects to other cmdlets such as the Import/Remove-Module cmdlets.
Get-Module does not require parameters. It only retrieves modules that have been imported into the current session. You can specify the -ListAvailable parameter to get all installed modules.
Get-Module imports modules but not modules. Modules are automatically imported starting in Windows PowerShell 3.0 when you use a command within the module. However, a Get-Module command doesn’t trigger an automatic import. The Import-Module cmdlet allows you to import modules into your session.
Windows PowerShell 3.0 allows you to import modules from remote sessions into your local session. This strategy uses PowerShell’s Implicit Remoting feature and is similar to using the Import–PSSession cmdlet. If you use commands from modules imported from another session to run the commands implicitly in the remote session, This feature allows you to manage the remote computer via the local session.
You can also use Windows PowerShell 3.0’s Get-Module or Import-Module to import and get Common Information Model (CIM), modules. The cmdlets are defined within Cmdlet Definition XML files (CDXML). This feature allows you to use cmdlets that have been implemented in non-managed code assembly, such as C++.
What version of PowerShell should I use for this blog?
Get the PowerShell Version for your machine
This command displays the PowerShell version information for your machine.

How to use the Get-Module
Modules can be imported into the current session
This command imports modules into the current session.

Install modules and find available modules
Get-Module -ListAvailable
This command will retrieve the modules installed on your computer and can be imported to the current session.
Get-Module searches for available modules on the path specified in the $env.PSModulePath environment variable.

You can get a module by its fully qualified title:
$FullyQualifedName = @ModuleName=”Microsoft.PowerShell.Management”;ModuleVersion=””
Get-Module -FullyQualifiedName $FullyQualifedName | Format-Table -Property Name,Version
This command gets the Microsoft.PowerShell.Management module by specifying the fully qualified name of the module by using the -FullyQualifiedName parameter.
The command pipes the results to the Format-Table cmdlet, which formats the results as a Table with Name and Version as column headings.

Display the contents of a module-specific manifest:
$m = Get Module -list Name BitsTransfer
Get-Content $m.Path
These commands display the contents for the PowerShell BitsTransfer Module module’s module manifest.
Modules do not need to have manifest files. If they do not have a manifest, it is only required to include a version number. However, manifest files can provide useful information about a module’s requirements and its contents.

The first command returns the PSModuleInfo object representing BitsTransfer module. It saves the object in $m variable.
The second command uses Get-Content to retrieve the contents of the specified manifest file. It uses dot notation in order to find the path to the manifest files, which are stored in the Path property. The output shows the contents for the module manifest.
Install modules on a computer
$s = New-PSSession-ComputerNameSCCMSSPRIME
Get-Module -PSSession $s -ListAvailable
The first command uses New-PSSession cmdlet for creating a PSSession in the SCCMSSPRIME. This command saves the PSSession to the $s variable.
The second command uses -PSSession, -ListAvailable parameters from Get-Module in order to retrieve the modules in the $s variable.
If modules are piped from other sessions to Import-Module cmdlet Import-Module imports them into the current session using the implicit remote feature.
This is the equivalent of using the Import–PSSession cmdlet. The module cmdlets can be used in the current session. However, remote commands that use these cmdlets will run in the remote session.

Get-PSSession: Last week’s command
Do you need PowerShell training? ITProTV offers PowerShell online IT training courses.