Skip to content

Importing PowerShell Modules

How to load, auto-load, and manage PowerShell modules using Import-Module, Remove-Module, and module auto-loading preferences for efficient script execution.

Before a module's functions, cmdlets, aliases, and variables can be used, they must be loaded into the current PowerShell session. PowerShell handles this process explicitly via Import-Module, automatically via module auto-loading when a command from an available module is called, and unloading via Remove-Module when no longer needed.

Loading Modules with Import-Module

The Import-Module cmdlet adds one or more modules to the current PowerShell session. You can import modules by name (if they reside in $env:PSModulePath) or by specifying a direct file path to a .psd1 manifest or .psm1 script module file.

Consolepowershell
Import-Module -Name "MyTools"

where -Name is the name of the module to import. PowerShell searches $env:PSModulePath for a matching module.

Consolepowershell
Get-Command -Module "MyTools"
Outputplaintext
CommandType     Name                    Version    Source
-----------     ----                    -------    ------
Function        Get-DiskSpaceReport     1.0.0      MyTools

Importing from a File Path

If a module isn't installed in a standard $env:PSModulePath location, import it directly by path using -Name with the full path, or by pointing at the .psm1/.psd1 file.

Consolepowershell
Import-Module -Name "C:\Scripts\Modules\MyTools\MyTools.psd1"

where -Name accepts either a module name (searched in $env:PSModulePath) or a direct path to a .psm1/.psd1 file.

Force Re-importing a Module

If a module has been updated on disk since it was last imported, use -Force to reload it.

Consolepowershell
Import-Module -Name "MyTools" -Force

where -Force re-imports the module even if a version is already loaded in the session, refreshing its functions to reflect any recent changes.

Advanced Import Parameters

Import-Module supports several parameters for controlling how a module is loaded and how its commands are exposed.

import-module-demo.ps1powershell
$module = Import-Module -Name ".\Modules\StorageTools\StorageTools.psd1" `
    -Prefix "Corp" `
    -PassThru `
    -Verbose

Get-Command -Module $module.Name

where:

  • Import-Module is the cmdlet used to load modules into the current runtime session.
  • -Name specifies the friendly name of an installed module or the file path to a module manifest (.psd1) or script module (.psm1).
  • -Prefix appends a prefix to the noun of all imported cmdlets/functions (e.g., Get-DiskReport becomes Get-CorpDiskReport), preventing naming collisions with existing commands.
  • -PassThru emits a PSModuleInfo object representing the imported module.
  • -Verbose prints informational messages detailing each function, alias, and format file processed during the import.
  • Get-Command -Module lists all commands exported by the specified module into the active session.
Consolepowershell
./import-module-demo.ps1
Outputplaintext
VERBOSE: Loading module from path 'C:\Scripts\Modules\StorageTools\StorageTools.psd1'.
VERBOSE: Exporting function 'Get-CorpDiskReport'.
VERBOSE: Exporting function 'Clear-CorpOldLogs'.

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Clear-CorpOldLogs                                  1.0.0      StorageTools
Function        Get-CorpDiskReport                                 1.0.0      StorageTools

Auto-Loading Modules

PowerShell automatically imports an installed module the first time any of its exported commands are invoked. Explicit Import-Module statements are typically unnecessary for interactive console work if the module resides in a folder listed under $env:PSModulePath. This is called module auto-loading.

Consolepowershell
Get-ScheduledTask
Outputplaintext
TaskPath  TaskName            State
--------  ------------------  -----
\         DailyBackup         Ready

Configuring Auto-Loading Behavior

Auto-loading behavior is controlled by the $PSModuleAutoLoadingPreference automatic variable.

autoload-config.ps1powershell
Write-Output "Current Preference: $PSModuleAutoLoadingPreference"

$PSModuleAutoLoadingPreference = 'ModuleQualified'

Write-Output "Updated Preference: $PSModuleAutoLoadingPreference"

where:

  • $PSModuleAutoLoadingPreference controls shell behavior when an unrecognized command is called. Supported values:
    • All (default): modules auto-load whenever one of their commands is invoked.
    • ModuleQualified: modules only auto-load when called with a fully qualified name (ModuleName\CommandName).
    • None: disables auto-loading entirely; modules must be imported manually.
Consolepowershell
./autoload-config.ps1
Outputplaintext
Current Preference: All
Updated Preference: ModuleQualified

Removing Modules with Remove-Module

The Remove-Module cmdlet unloads a module's commands from the current session without uninstalling it from disk. It removes all exported functions, aliases, and module-scoped variables from the active session.

Consolepowershell
Remove-Module -Name "MyTools"

where -Name the name of the currently loaded module to remove.

Consolepowershell
Get-Command -Module "MyTools"

Verifying Module Removal

remove-module-demo.ps1powershell
$isLoadedBefore = [bool](Get-Module -Name "StorageTools")

Remove-Module -Name "StorageTools" -Verbose

$isLoadedAfter = [bool](Get-Module -Name "StorageTools")

[PSCustomObject]@{
    Module          = "StorageTools"
    LoadedBefore    = $isLoadedBefore
    LoadedAfter     = $isLoadedAfter
}

where:

  • Remove-Module unloads the specified module from memory.
  • -Name specifies the name of the loaded module to remove.
  • -Verbose outputs real-time removal steps and confirmations.
  • [bool](Get-Module ...) casts the result of Get-Module to $true if loaded, or $false if absent.
Consolepowershell
./remove-module-demo.ps1
Outputplaintext
VERBOSE: Performing the operation "Remove-Module" on target "StorageTools (Path: 'C:\Scripts\Modules\StorageTools\StorageTools.psd1')".

Module       LoadedBefore LoadedAfter
------       ------------ -----------
StorageTools         True       False

Practical Usage

Reloading a Module During Development

When actively developing a module, combine Remove-Module and Import-Module -Force to guarantee a clean reload, avoiding stale cached versions of functions.

reload-module.ps1powershell
param([string]$ModuleName)

if (Get-Module -Name $ModuleName) {
    Remove-Module -Name $ModuleName -Force
}

Import-Module -Name $ModuleName -Force
Write-Output "$ModuleName reloaded successfully."

where:

  • Get-Module -Name $ModuleName checks if the module is currently loaded.
  • Remove-Module -Force unloads it if present.
  • Import-Module -Force reloads the module fresh from disk.
Consolepowershell
./reload-module.ps1 -ModuleName "MyTools"
Outputplaintext
MyTools reloaded successfully.

Managing Version Conflicts

When multiple versions of the same module exist side-by-side in $env:PSModulePath, PowerShell defaults to loading the highest version number available. You can use Import-Module parameters to explicitly control which version is loaded.

import-version.ps1powershell
Get-Module -ListAvailable -Name "Az.Accounts" | 
    Select-Object -Property Name, Version, Path

Import-Module -Name "Az.Accounts" -RequiredVersion "2.12.1"

(Get-Module -Name "Az.Accounts").Version.ToString()

where:

  • Get-Module -ListAvailable catalogs every installed version of the module across all $env:PSModulePath locations.
  • -RequiredVersion forces Import-Module to import an exact matching version number instead of automatically defaulting to the highest release.
  • (Get-Module -Name "Az.Accounts").Version accesses the System.Version object of the active in-memory module.
Consolepowershell
./import-version.ps1
Outputplaintext
Name        Version Path
----        ------- ----
Az.Accounts 2.12.1  C:\Program Files\PowerShell\Modules\Az.Accounts\2.12.1\Az.Accounts.psd1
Az.Accounts 3.0.0   C:\Program Files\PowerShell\Modules\Az.Accounts\3.0.0\Az.Accounts.psd1

2.12.1
Navigation