Furthermore, the Modules can be imported into the current session of PowerShell. To do so, PowerShell provides support for the Import-Module cmdlet. In this informative post, we have provided the fundamentals of the Import-Module cmdlet and a few examples are stated that demonstrate the way to import a module using the Import-Module cmdlet.
How Import-Module works
PowerShell’s cmdlets and functions do follow a syntax to use them.
Note: The Import-Module loads all the members of a module automatically. However, you can import a particular member of the module as well by using the specific parameter.
Before importing modules, it is recommended to look for the modules that are imported into the PowerShell session at startup. To get the list, use the below-stated command:
How to use the Import-Module cmdlet in PowerShell
Whenever a cmdlet or a function is executed, PowerShell starts adding relevant modules to the current session. However, you can import targeted modules by using the Import-Module cmdlet. The upcoming examples practice Import-Module cmdlet:
Example 1: Importing all members of a module
The Microsoft.PowerShell.Management module handles the most used cmdlets and functions, like Get-Content, Get-ChildItem, Get-Process, Get-Service, Debug-Process, and so on. The command written below adds Microsoft.PowerShell.Management module to the current session of PowerShell.
And once the Import-Module command is executed successfully, it is observed that all the members of Microsoft.PowerShell.Management is imported.
Note: The verbose option in the above-mentioned command lists the members of a module that are being imported.
Example 2: Import specific member(s)
Using the Import-Module cmdlet, it is possible to get only one member or a few members by mentioning their names. As an example, let’s say that if we want to import the “gin” member of Microsoft.PowerShell.Management. The “gin” is an alias of the Get-ComputerInfo cmdlet of PowerShell which shows information about a computer. To do so, we have used the Alias parameter of the Import-Module command in the following way:
Example 3: Using Import-Module to prefix a word before each member
PowerShell allows you to get the members of a module by prefixing a word to each. A set of members of a module can be imported by using a prefix. A prefix parameter accepts alphabets and prefixes those alphabets before each member. For instance, the command is written below prefixes “con” before each member of Microsoft.PowerShell.Management. The prefix parameter helps to create another name of that cmdlet.
By prefixing a word before each member, you can execute a cmdlet or alias by using the original and the prefixed member. In our case, we have prefixed “Con” to each member so, Get-Process and Get-ConProcess would print the same result. It can be verified from the command written below as we have executed Get-ConProcess and the result displays the number of processes as of Get-process.
Example 4: Using Import-Module with version limit
The version of a member can also be considered to import a module using Import-Module. The MinimumVersion parameter of the Import-module limits the version of the member. For instance, the following command only imports members that have versions greater than 3.0.0.
Moreover, there is another parameter that filters the importing of modules by using the RequiredVersion parameter of Import-Module.
The RequiredVersion parameter of Import-Module allows you to import the members that match the specific version. Here, we are importing those members that have version 7.0.0.0 and the following command assists us in this regard:
Conclusion
A PowerShell module contains cmdlets, functions, scripts, variables, and more. As most of the automating tasks depend on cmdlets and functions, therefore modules play a crucial role in PowerShell functionalities. This write-up provides a detailed explanation and demonstration of the Import-Module cmdlet in PowerShell. You have learned to import a module and all its members using a single command. However, Import-Cmdlet supports several parameters like RequiredVersion and Alias to get only specific members of a module.