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 Import-Module .
When to use Import-Module?
The Import-Module cmdlet adds one or more modules to the current session. Starting in PowerShell 3.0, installed modules are automatically imported to the session when you use any commands or providers in the module. However, you can still use the Import-Module command to import a module. You can disable automatic module importing using the $PSModuleAutoloadingPreference preference variable.
A module is a package that contains members that can be used in PowerShell. Members include cmdlets, providers, scripts, functions, variables, and other tools and files. After a module is imported, you can use the module members in your session.
By default, Import-Module imports all members that the module exports, but you can use the Alias, Function, Cmdlet, and Variable parameters to restrict which members are imported. The –NoClobber parameter prevents Import-Module from importing members that have the same names as members in the current session.
Import-Module imports a module only into the current session. To import the module into every new session, add an Import-Module command to your PowerShell profile.
You can manage remote Windows computers that have PowerShell remoting enabled by creating a PSSession on the remote computer. Then use the –PSSession parameter of Import-Module to import the modules that are installed on the remote computer. You can now use the imported commands in the current session. The commands implicitly run on the remote computer.
Starting in Windows PowerShell 3.0, you can use Import-Module to import Common Information Model (CIM) modules, in which the cmdlets are defined in Cmdlet Definition XML (CDXML) files. This feature allows you to use cmdlets that are implemented in non-managed code assemblies, such as those written in C++.
For remote computers that do not have PowerShell remoting enabled, including computers that are not running the Windows operating system, you can use the CIMSession parameter of Import-Module to import CIM modules from the remote computer. The imported commands run implicitly on the remote computer. A CIMSession is a connection to Windows Management Instrumentation (WMI) on the remote computer.
What version of PowerShell am I using?
Get the PowerShell Version from your machine:
This command shows you the PowerShell version information on your machine.
How to use Import-Module?
Import the members of a module into the current session:
Import-Module -Name PSDiagnostics
This example imports the members of the PSDiagnostics module into the current session.
Import all modules specified by the module path:
Get-Module -ListAvailable | Import-Module
This example imports all available modules in the path specified by the $env:PSModulePath
environment variable into the current session.
Import the members of several modules into the current session:
$m = Get-Module -ListAvailable PSDiagnostics, Dism
Import-Module -ModuleInfo $m
The Get-Module cmdlet gets the PSDiagnostics and Dism modules and saves the objects in the $m variable.
The –ListAvailable parameter is required when you are getting modules that are not yet imported into the session.
The –ModuleInfo parameter of Import-Module is used to import the modules into the current session.
Restrict module members imported into a session:
Import-Module PSDiagnostics -Function Disable-PSTrace, Enable-PSTrace
Get-Command -Module PSDiagnostics
The –Function parameter limits the members that are imported from the module. You can also use the Alias, Variable, and Cmdlet parameters to restrict other members that a module imports.
The Get-Module cmdlet gets the object that represents the PSDiagnostics module.
The ExportedCommands property lists all the cmdlets that the module exports, even though they were not all imported.
Using the –Module parameter of the Get-Command cmdlet shows the commands that were imported from the PSDiagnostics module.
The results confirm that only the Disable-PSTrace and Enable-PSTrace cmdlets were imported.
Import the members of a module and add a prefix:
Import-Module PSDiagnostics -Prefix x -PassThru
Get-Command -Module PSDiagnostics
This example imports the PSDiagnostics module into the current session, adds a prefix to the member names, and then displays the prefixed member names.
The –Prefix parameter of Import-Module adds the x prefix to all members that are imported from the module. The prefix applies only to the members in the current session. It does not change the module.
The –PassThru parameter returns a module object that represents the imported module.
Get-Command gets the members that have been imported from the module. The output shows that the module members were correctly prefixed.
Import a minimum version of a module:
Import-Module -Name PowerShellGet -MinimumVersion 2.0.0
This example imports the PowerShellGet module. It uses the –MinimumVersion parameter of Import-Module to import only version 2.0.0 or greater of the module.
NOTE: You can also use the –RequiredVersion parameter to import a particular version of a module, or use the –Module and –Version parameters of the #Requires keyword to require a particular version of a module in a script.
Learn last week’s command: Enable-PSRemoting.
Need PowerShell training? Check out ITProTV’s PowerShell online IT training courses.