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.
Import-Module -Name "MyTools"where -Name is the name of the module to import. PowerShell searches $env:PSModulePath for a matching module.
Get-Command -Module "MyTools"CommandType Name Version Source
----------- ---- ------- ------
Function Get-DiskSpaceReport 1.0.0 MyToolsImporting 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.
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.
Import-Module -Name "MyTools" -Forcewhere -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.
$module = Import-Module -Name ".\Modules\StorageTools\StorageTools.psd1" `
-Prefix "Corp" `
-PassThru `
-Verbose
Get-Command -Module $module.Namewhere:
Import-Moduleis the cmdlet used to load modules into the current runtime session.-Namespecifies the friendly name of an installed module or the file path to a module manifest (.psd1) or script module (.psm1).-Prefixappends a prefix to the noun of all imported cmdlets/functions (e.g.,Get-DiskReportbecomesGet-CorpDiskReport), preventing naming collisions with existing commands.-PassThruemits aPSModuleInfoobject representing the imported module.-Verboseprints informational messages detailing each function, alias, and format file processed during the import.Get-Command -Modulelists all commands exported by the specified module into the active session.
./import-module-demo.ps1VERBOSE: 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 StorageToolsAuto-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.
Get-ScheduledTaskTaskPath TaskName State
-------- ------------------ -----
\ DailyBackup ReadyConfiguring Auto-Loading Behavior
Auto-loading behavior is controlled by the $PSModuleAutoLoadingPreference automatic variable.
Write-Output "Current Preference: $PSModuleAutoLoadingPreference"
$PSModuleAutoLoadingPreference = 'ModuleQualified'
Write-Output "Updated Preference: $PSModuleAutoLoadingPreference"where:
$PSModuleAutoLoadingPreferencecontrols 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.
./autoload-config.ps1Current Preference: All
Updated Preference: ModuleQualifiedRemoving 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.
Remove-Module -Name "MyTools"where -Name the name of the currently loaded module to remove.
Get-Command -Module "MyTools"Verifying Module Removal
$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-Moduleunloads the specified module from memory.-Namespecifies the name of the loaded module to remove.-Verboseoutputs real-time removal steps and confirmations.[bool](Get-Module ...)casts the result ofGet-Moduleto$trueif loaded, or$falseif absent.
./remove-module-demo.ps1VERBOSE: Performing the operation "Remove-Module" on target "StorageTools (Path: 'C:\Scripts\Modules\StorageTools\StorageTools.psd1')".
Module LoadedBefore LoadedAfter
------ ------------ -----------
StorageTools True FalsePractical 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.
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 $ModuleNamechecks if the module is currently loaded.Remove-Module -Forceunloads it if present.Import-Module -Forcereloads the module fresh from disk.
./reload-module.ps1 -ModuleName "MyTools"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.
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 -ListAvailablecatalogs every installed version of the module across all$env:PSModulePathlocations.-RequiredVersionforcesImport-Moduleto import an exact matching version number instead of automatically defaulting to the highest release.(Get-Module -Name "Az.Accounts").Versionaccesses theSystem.Versionobject of the active in-memory module.
./import-version.ps1Name 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