Get-Module | Taking on PowerShell one cmdlet at a time | Weekly Blog

Share this post:

This is a part of an on-going blog series written by Adam Gordon. Each week, Adam will walk you through a PowerShell command, showing you when and how to use each one. This week, Adam covers Get-Module.

When to use Get-Module?

The Get-Module cmdlet gets the PowerShell modules that have been imported, or that can be imported, into a PowerShell session. The module object that Get-Module returns contains valuable information about the module. You can also pipe the module objects to other cmdlets, such as the Import-Module and Remove-Module cmdlets.

Without parameters, Get-Module gets modules that have been imported into the current session. To get all installed modules, specify the –ListAvailable parameter.

Get-Module gets modules, but it does not import them. Starting in Windows PowerShell 3.0, modules are automatically imported when you use a command in the module, but a Get-Module command does not trigger an automatic import. You can also import the modules into your session by using the Import-Module cmdlet.

Starting in Windows PowerShell 3.0, you can get and then, import modules from remote sessions into the local session. This strategy uses the Implicit Remoting feature of PowerShell and is equivalent to using the Import-PSSession cmdlet. When you use commands in modules imported from another session, the commands run implicitly in the remote session. This feature lets you manage the remote computer from the local session.

Also, starting in Windows PowerShell 3.0, you can use Get-Module and Import-Module to get and import Common Information Model (CIM) modules, in which the cmdlets are defined in Cmdlet Definition XML (CDXML) files. This feature lets you use cmdlets that are implemented in non-managed code assemblies, such as those written in C++.

What version of PowerShell am I using for this blog?

Get the PowerShell Version from your machine:

$PSVersionTable

This command shows you the PowerShell version information on your machine.

How to use Get-Module?

Get modules imported into the current session: 

Get-Module

This command gets modules that have been imported into the current session.

Get installed modules and available modules:

Get-Module -ListAvailable

This command gets the modules that are installed on the computer and can be imported into the current session.

Get-Module looks for available modules in the path specified by the $env:PSModulePath environment variable.

Get a module by its fully qualified name:

$FullyQualifedName = @{ModuleName=”Microsoft.PowerShell.Management”;ModuleVersion=”3.1.0.0″}

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 then pipes the results into the Format-Table cmdlet to format the results as a table with Name and Version as the column headings.

Display the contents of a module manifest:

$m = Get-Module -list -Name BitsTransfer

Get-Content $m.Path

These commands display the contents of the module manifest for the PowerShell BitsTransfer module.

Modules are not required to have manifest files. When they do have a manifest file, the manifest file is required only to include a version number. However, manifest files often provide useful information about a module, its requirements, and its contents.

The first command gets the PSModuleInfo object that represents BitsTransfer module. It saves the object in the $m variable.

The second command uses the Get-Content cmdlet to get the content of the manifest file in the specified path. It uses dot notation to get the path to the manifest file, which is stored in the Path property of the object. The output shows the contents of the module manifest.

Get modules installed on a computer:

$s = New-PSSession -ComputerName SCCMSSPRIME

Get-Module -PSSession $s -ListAvailable

The first command uses the New-PSSession cmdlet to create a PSSession on the SCCMSSPRIME computer. The command saves the PSSession in the $s variable.

The second command uses the –PSSession and –ListAvailable parameters of Get-Module to get the modules in the PSSession in the $s variable.

If you pipe modules from other sessions to the Import-Module cmdlet, Import-Module imports the module into the current session by using the implicit remoting feature.

This is equivalent to using the Import-PSSession cmdlet. You can use the cmdlets from the module in the current session, but commands that use these cmdlets actually run in the remote session.

Learn last week’s command: Get-PSSession.

Need PowerShell training? Check out ITProTV’s PowerShell online IT training courses.

Leave a Reply

Your email address will not be published. Required fields are marked *