Taking on PowerShell one cmdlet at a time

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

When should you use Import-Module
The Import-Module cmdlet adds a module to the current session. PowerShell 3.0 automatically imports installed modules to your session whenever you use any provider or commands in the module. To import a module, you can still use Import-Module. You can disable automatic module importing using the $PSModuleAutoloadingPreference preference variable.
A module is a package that contains members which can be used with PowerShell. Members can include cmdlets and providers, scripts and functions, as well as files and other tools. Once a module has been imported, you can use its members in your session.
Import-Module will import all members exported by the module by default. However, you can restrict which members are imported using the Alias and Function parameters. The -NoClobber parameter stops Import-Module importing members with the same names as the members in the current session.
Import-Module imports the module into the current session. Add an Import-Module command in your PowerShell profile to import the module into each new session.
Remote Windows computers can be managed by PowerShell remote access. To do this, create a PSSession. Next, use the -PSSession parameter in Import-Module for the import of the modules on the remote computer. The imported commands can now be used in the current session. The commands are implicitly executed on the remote computer.
Windows PowerShell 3.0 now supports Import-Module. This allows you to import Common Information Model modules (CIM). The cmdlets are defined within Cmdlet Definition XML files (CDXML). This feature allows you use cmdlets that have been implemented in non-managed code assembly, such as C++.
Remote computers without PowerShell remoting enable, or computers that are not running Windows, can use the CIMSession parameter in Import-Module. This allows you to import CIM modules from remote computers. The remote computer runs the imported commands implicitly. A CIMSession connects to Windows Management Instrumentation (WMI), on the remote computer.
What version of PowerShell do I use?
Get the PowerShell Version for your machine
$PSVersionTable
This command displays the PowerShell version information for your machine.

How to use the Import-Module
Import the members from a module into the current session
Import-Module -Name PSDiagnostics

This example imports members of the PSDiagnostics Module into the current session.
Import all modules specified in the module path
Get-Module -ListAvailable | Import-Module

This example imports all modules available in the path specified with the $env.PSModulePath
Environment variable to the current session.
Import members from several modules into the current session
$m = Get-Module -ListAvailable PSDiagnostics, Dism
Import-Module -ModuleInfo $m

The Get-Module cmdlet retrieves the PSDiagnostics & Dism modules, and saves the objects in $m.
When you receive modules that have not been imported into your session, the -ListAvailable parameter must be used.
The -ModuleInfo parameter in Import-Module allows you to import modules into the current session.

Restrict module members imported to a session
Import-Module PSDiagnostics -Function Disable-PSTrace, Enable-PSTrace
(Get-Module PSDiagnostics).ExportedCommands
Get-Command Module PSDiagnostics

The -Function parameter restricts the members that can be imported from the module. To limit other members that a module imports, you can also use the Alias and Variable parameters.
The Get-Module cmdlet retrieves the object that represents PSDiagnostics module.
The ExportedCommands property lists all cmdlets that the module exports even though they weren’t all imported.
The -Module parameter in the Get-Command cmdlet displays the commands that were imported into the PSDiagnostics Module.
The results show that only the Disable–PSTrace and Enable–PSTrace cmdlets have been imported.
Import the module members and add a prefix
Import-Module PSDiagnostics-Prefixx -PassThru
Get-Command Module PSDiagnostics

This example imports the PSDiagnostics Module into the current session, adds an additional prefix to the member name, and displays the prefixed member names.
The -Prefix parameter in Import-Module adds an x prefix for all members imported from the module. The prefix is only applicable to members in the current session. It does not alter the module.
The -PassThru parameter returns an object module that represents the imported module.
Get-Command allows you to import members

You Might Also Like