Updated March 14, 2023
Introduction to Powershell Module Path
The locations of the modules that are currently installed on the system are stored in the PSModulePath environment variable. There are two important ways of expanding the PowerShell cmdlets; they are Modules and Snapins. Both are packages of cmdlets and functions. Snapins were the first ones to be developed, and they were introduced with PowerShell version 1, whereas modules were introduced in PowerShell version 2. Snapins are being deprecated. Snapins are defined in assemblies, whereas modules are defined in assemblies or PowerShell scripts. Both snapins and modules are nothing but a package of cmdlets, functions, variables, and workflows. The registered snapins can find out by using the “Get-PSSnapin-registered” cmdlet. The list of available modules installed can find out by using the “Get-Module –ListAvailable” cmdlet. This article will cover Powershell Module Path in detail.
List of Modules available at Directory: C:\Program Files\WindowsPowerShell\Modules
ModuleType | Version | Name |
Script | 1.0.1 | Microsoft.PowerShell.Operation.Validation |
Binary | 1.0.0.1 | PackageManagement |
Script | 3.4.0 | Pester |
Script | 1.0.0.1 | PowerShellGet |
Script | 1.2 | PSReadline |
List of Modules available at Directory Directory:C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
ModuleType | Version | Name |
Manifest | 1.0.0.0 | AppBackgroundTask |
Manifest | 2.0.0.0 | AppLocker |
Manifest | 1.0.0.0 | AppvClient |
Manifest | 2.0.0.0 | Appx |
Script | 1.0.0.0 | AssignedAccess |
Manifest | 1.0.0.0 | BitLocker |
Manifest | 2.0.0.0 | BitsTransfer |
Manifest | 1.0.0.0 | BranchCache |
Manifest | 1.0.0.0 | CimCmdlets |
Manifest | 1 | ConfigCI |
Manifest | 1 | Defender |
Manifest | 1.0.0.0 | DeliveryOptimization |
Manifest | 1.0.0.0 | DirectAccessClientComponents |
Script | 3 | Dism |
Manifest | 1.0.0.0 | DnsClient |
Manifest | 1.0.0.0 | EventTracingManagement |
Manifest | 2.0.0.0 | International |
Manifest | 1.0.0.0 | iSCSI |
Script | 1.0.0.0 | ISE |
To get only the modules that are available in the current session, the cmdlet “Get-Module -All” is used. Any module can be imported into a session with the help of the Import-Module cmdlet.
Modules in the current session:
ModuleType | Version | Name |
Script | 1.0.0.0 | ISE |
Binary | 3.0.0.0 | Microsoft.PowerShell.Commands.Management.dll |
Binary | 3.0.0.0 | Microsoft.PowerShell.Commands.Utility.dll |
Manifest | 3.1.0.0 | Microsoft.PowerShell.Management |
Script | 0 | Microsoft.PowerShell.Utility |
Manifest | 3.1.0.0 | Microsoft.PowerShell.Utility |
Example:
Import-Module -Name PSDiagnostics
The verbose parameter can be used to know about the cmdlets and functions available in the module while the module is being imported.
To remove the module from the session, the below cmdlet can be used
Remove-Module-Name PSDiagnostics. The verbose parameter can be used here also.
PSModulePath:
The PSModulePath is an environment variable stores the address of the modules that are installed on the system. When the user is not aware of the path of the module or doesn’t specify, PowerShell uses this variable to locate them. To view the modules that are stored, execute the following cmdlet.
Syntax:
$env:PSModulePath
Output:
The default value of the $env:PSModulePath is one of the following values
- $HOME\Documents\PowerShell\Modules;
- $PSHOME\Modules or $HOME\Documents\WindowsPowerShell\Modules;
- $PSHOME\Modules
Working with PSModulePath
A temporary new path or location can be added to the PSModulePath as follows.
$env:PSModulePath = $env:PSModulePath + ";c:\windows\test"
A new variable to the registry can be added by using the environment variable editor.
To add a new variable using the script, the SetEnvironmentVariable method of the Environment class must be used.
$CurrentValue = [Environment]::GetEnvironmentVariable("PSModulePath", "Machine")
[Environment]::SetEnvironmentVariable("PSModulePath", $CurrentValue + ";C:\vignesh\test", "Machine")
To remove path or location from the PSModulePath, the replace parameter can be used.
$env:PSModulePath -replace ";c:\windows\test".
The above cmdlet will remove c:\windows\test from modulepath.
Enabling Automatic Import of Modules:
By default, the modules that are available in the PSModulePath are only imported in a session. To import modules from other locations, the Import-Module cmdlet must be used. $PSModuleAutoloadingPreference is the parameter that determines whether modules are to be automatically imported or not. Its acceptable values are All, ModuleQualified, and None.
All: It is the default value. Modules are imported for first-time use.
ModuleQualified: Only if the user specifies a module, then it is imported.
None: Modules are not automatically imported. E.g. $PSModuleAutoloadingPreference = ‘none’
Examples:
To add a new location of a module to PSModulePath.
Input :
Function Add-ToPSModulePathEg {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)][String]$ModulePath
)
Write-Host "Add a path tutorial"
if (!($env:PSModulePath.split(";") -contains $ModulePath)){
$Current = $env:PSModulePath
[Environment]::SetEnvironmentVariable("PSModulePath",$Current + ";" + $ModulePath, "Machine")
$env:PSModulePath = [System.Environment]::GetEnvironmentVariable("PSModulePath","Machine")
Write-Host "Path Added"
}else{
Write-Host "$ModulePath is already available in psMOdulePath $env:psModulePath"
}
}
Steps on how to run the script:
The above script is a function. So parameters need to be passed to it.
First, run the script.
In the output pane, type Add-ToPSModulePathEg – ModulePath “Path to be added here.”
See the below output screen for reference
Output:
In the above output, if you see when the first time the script is run. The path, if it is not there already in the ModulePath, will be added, and Add a path tutorial will be printed along with the path added. If the script is run for the same path again, then the second output, “Test4 is already available,” is printed.
To remove a path from PSModulePath:
Input:
function Remove-FromPSModulePathEg{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)][String]$ModulePath
)
Write-Host "Removing a path from PSModulePath Variable"
if ($env:PSModulePath.split(";") -contains $ModulePath){
$NewValue = (($env:PSModulePath).Split(";") | ? { $_ -ne $ModulePath }) -join ";"
[Environment]::SetEnvironmentVariable("PSModulePath", $NewValue, "Machine")
$env:PSModulePath = [System.Environment]::GetEnvironmentVariable("PSModulePath","Machine")
write-Host "$ModulePath removed. Restart the prompt for the changes to take effect."
}else{
write-Host "$ModulePath is not present in $env:psModulePath"
}
}
Steps on how to run the script:
The above script is a function. So parameters need to be passed to it.
First, run the script.
In the output pane, type FromPSModulePathEg – ModulePath “Path to be deleted here.”
See the below output screen for reference
Output:
In the above output, first, the above-added path is removed. Next, removing a path from the output is produced. Finally, the second time the script is run for the removed path, the path that is not present is produced.
Adding Multiple Locations to the PSModulePath Simultaneously
Input:
Write-Host "Welcome to adding multiple paths to the PSModulePath Environment Variable"
#storing the paths to be added in an array
$address=@("C:\Vignesh\test","C:\Vignesh\Tickets","C:\Vignesh\Patching","C:\Vignesh\Training Screenshots" )
foreach($add in $address)
{
Write-Host "Adding the path" $add "to the PSModulePath Variable"
$env:PSModulePath = $env:PSModulePath +";$add"
Write-Host "Path added"
Write-Host "Current value of PSModulePath is "$env:PSModulePath
}
Output:
Removing Multiple Paths from the PSModulePath Simultaneously:
Input:
Write-Host "Welcome to deleting multiple paths from the PSModulePath Environment Variable"
Write-Host "Current Value in PSModulePath is" $env:PSModulePath
#storing the paths to be removed from the location
$address=@("C:\Vignesh\test","C:\Vignesh\Tickets","C:\Vignesh\Patching","C:\Vignesh\Training Screenshots" )
foreach($add in $address)
{
if ($env:PSModulePath.split(";") -contains $add){
Write-Host "Removing the path" $add
$NewValue = (($env:PSModulePath).Split(";") | ? { $_ -ne $add }) -join ";"
[Environment]::SetEnvironmentVariable("PSModulePath", $NewValue, "Machine")
$env:PSModulePath = [System.Environment]::GetEnvironmentVariable("PSModulePath","Machine")
write-Host "$add removed"
}
}
Output:
Conclusion – Powershell Module Path
Thus, the article covered in detail the PSModulePath environment variable. It also explained the difference between the module and snapin. Some key points to be noted are when auto-loading of the module is enabled; it should be taken care that the first position has the required module. In the case of multiple versions for the same module, PowerShell, by default, uses the latest version of the module. It also demonstrated examples of how to add and remove to a PSModulePath variable. To learn in-depth about the cmdlet, creating sample programs, and having fun working around them is advisable.
Recommended Articles
This is a guide to Powershell Module Path. Here we discuss the PSModulePath environment variable and detail about the PSModulePath environment variable. It also explained. You may also look at the following articles to learn more –