How to Use Import-Module in PowerShell: Simple Guide
Use
Import-Module in PowerShell to load a module into your session, making its commands available. Simply run Import-Module ModuleName to import the module by name or path.Syntax
The basic syntax of Import-Module is simple:
Import-Module <ModuleNameOrPath>: Loads the specified module by name or file path.-Force: Reloads the module even if it is already loaded.-Verbose: Shows detailed information during import.
powershell
Import-Module ModuleName [-Force] [-Verbose]
Example
This example shows how to import the built-in Microsoft.PowerShell.Utility module and then use a command from it.
powershell
Import-Module Microsoft.PowerShell.Utility
Get-Command -Module Microsoft.PowerShell.Utility | Select-Object -First 3Output
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Add-Member 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Clear-Content 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Clear-Item 3.1.0.0 Microsoft.PowerShell.Utility
Common Pitfalls
Common mistakes when using Import-Module include:
- Trying to import a module that is not installed or not in the module path.
- Forgetting to specify the full path if the module is not in a standard location.
- Not using
-Forcewhen you want to reload an updated module.
Example of a wrong and right way:
powershell
# Wrong: Module not found error
Import-Module NonExistentModule
# Right: Import a module by full path
Import-Module "C:\Modules\MyModule.psm1"Output
Import-Module : The specified module 'NonExistentModule' was not loaded because no valid module file was found in any module directory.
At line:1 char:1
+ Import-Module NonExistentModule
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (NonExistentModule:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Quick Reference
| Parameter | Description |
|---|---|
| ModuleNameOrPath | Name or path of the module to import |
| -Force | Reloads the module even if already loaded |
| -Verbose | Shows detailed import process information |
| -PassThru | Returns the imported module object |
| -DisableNameChecking | Suppresses warnings about unapproved verbs |
Key Takeaways
Use Import-Module to load modules and access their commands in your PowerShell session.
Specify the module name or full path if the module is not in a standard location.
Use -Force to reload a module if it is already loaded and you want the latest version.
Check for module availability to avoid errors when importing.
Use -Verbose to see detailed information during the import process.