Updated March 24, 2023
Introduction to PowerShell Import Module
A module is a collection of cmdlets, variables, functions, and workflows put together as a package. Modules were first introduced in PowerShell version 2. Modules are generally stored in the following two primary locations. In this topic, we are going to learn about PowerShell Import Module.
- %windir%\system32\WindowsPowerShell\v1.0\Modules this is the location for system-wide modules available to any user in the system.
Each module has a dedicated folder in which it is saved. It also contains a psd1 file called Module Manifest. The manifest file contains the setting of the module like the version of PowerShell, author, and other settings. The list of available modules can get by running the Get-Module -ListAvailable cmdlet.
Importing a Module
Modules can be imported in a session by using the Import-Module cmdlet. For a module to be imported, it is necessary for it to be present on the system or on a remote server. Starting from PowerShell 3.0, Modules are automatically imported the first time any cmdlet in the installed module is run. $PSModuleAutoloadingPreference preference variable is used to enable or disable the automatic importing of modules. Import-Module cmdlets imports all the members by default, this can be restricted by using the alias or variable parameters.
Syntax of Import-Module
The following is the syntax of Import-Module Cmdlet
There are many parameters that are associated with the Import-Module Cmdlet, few of the commonly used parameters will be discussed below.
This denotes the list of aliases that will be imported from the module into the current session. Some module automatically exports the selected alias by default. Its type is a string and the default value is none. It doesn’t accept pipeline input and wild card characters. It is an optional parameter.
This is a mandatory parameter. This represents the names of the modules to be imported. The name can represent a module or a file inside the module such as a dll or .ps1 file. It’s wise always to use the module name. Its type is a string. The default value is none. It accepts pipeline input and wild card characters.
This specifies the prefix that will be added to the names of the imported module members. This affects the module members in the current session. Its type is a string and the default value is none. It doesn’t accept pipeline input and wild card characters.
It denotes the version of the module that needs to be imported, in case if the specified version is not available then an error is thrown. Its type is version and the default value is none. It doesn’t accept pipeline input and wild card characters.
It denotes the scope in which the module must be imported. It can be either global or local. In global it is available to all commands in the session and in local it is only for the current scope. Its type is a string and default value is none. It doesn’t accept pipeline input and wild card characters.
It denotes the list of variables that needs to be imported from the module into the current session. Its type is a string and default value is none. It doesn’t accept pipeline input, but wild card characters are accepted.
Here are the following examples of PowerShell Import Module mention below
Importing a module and getting the commands in the module
Write-Host "Welcome to Import Module Tutorial" -ForegroundColor Green
Write-Host "List of modules available in the current session are below" -ForegroundColor Green
Write-Host "Importing Diagnostics Module" -ForegroundColor Green
Import-Module -Name PSDiagnostics
Write-Host "Commands available in PSDiagnostics are follows" -ForegroundColor Green
Get-Command -Module PSDiagnostics -TotalCount 1
Importing a Module and Removing a Module along with the use of verbose:
The verbose parameter is used to list down the various scripts, functions, variables, and workflows that are available in the module being imported.
Write-Host "Welcome to verbose and removal of module tutorial" -ForegroundColor Green
Write-Host "Importing Diagnostics Module with verbose parameter" -ForegroundColor Green
Import-Module -Name PSDiagnostics -Verbose
Write-Host "Removing the module from the current session" -ForegroundColor Green
Remove-Module -Name PSDiagnostics
Write-Host "Successfully remove the Module"
Restricting the members that are being imported:
As said above, it is possible to restrict the members of a module from being imported. A module can have 100 commands and functions. It’s not advisable to import all of them when only a few of them are going to be used. It’s better to import only the needed members than importing all. The following example shows how to restrict the members.
Write-Host "Welcome to the tutorial of restricting cmdlets from being imported" -ForegroundColor Green
Write-Host "The following are the available cmdlets in the PSDiagnostics Module" -ForegroundColor Green
#mention only start and stop to be imported
Write-Host "Lets see how to import only start the trace and stop trace" -ForegroundColor Green
Import-Module PSDiagnostics -Function Start-Trace , Stop-Trace
Write-Host "only stop and start trace are imported" -ForegroundColor Green
Write-Host "imported cmdlets are as below" -ForegroundColor Green
Get-Command -Module PSDiagnostics
Re-importing a module, adding a prefix and retrieving only a specified version of a module:
Write-Host "welcome to prefix tutorial" -ForegroundColor Green
Write-Host "Importing PSDiagnostics for first time" -ForegroundColor Green
Write-Host "Module Imported"
Write-Host "Importing PSDiagnostics for second time with prefix" -ForegroundColor Green
Import-Module PSDiagnostics -Force -Prefix Px
Write-Host "List of commands with prefix px" -ForegroundColor Green
Get-Command -Module PSDiagnostics
Write-Host "Importing PowerShell get module with 3.0 version" -ForegroundColor Green
Import-Module -Name PowerShellGet -MinimumVersion 3.0.0
Write-Host "Module imported"
In the below the highlighted area shows the prefixed ones.
Conclusion – PowerShell Import Module
Thus, the article covered in detail about the Import-Module Cmdlet in detail. It also explained how a module can be imported, various ways of importing a module, filtering the commands and functions that need to be imported along with appropriate examples. To learn in-depth about the cmdlet it is advisable to create sample programs and have fun working around them.
This is a guide to PowerShell Import-Module. Here we discuss the examples of PowerShell Import-Module along with the Syntax and Parameters. You may also look at the following article.