Sometimes, work environments restrict PowerShell module installation in standard locations, even for the current user. In these cases, you may need to use modules from a custom folder.
However, Import-Module
can’t detect modules outside standard locations. To solve this, I’ve created a function that adds a custom folder to the PSModulePath
. This allows PowerShell to find and load modules from additional locations dynamically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<# .SYNOPSIS Adds a specified directory to the PSModulePath environment variable. .DESCRIPTION This function accepts a directory path and checks if it's already present in the $env:PSModulePath. If the path is not present, it adds the path to the beginning of the PSModulePath, ensuring that PowerShell modules in that directory can be found. If the path already exists, no changes are made. .PARAMETER PathToAdd The full directory path that you want to add to the PSModulePath environment variable. This is a mandatory parameter and must point to an existing directory. .EXAMPLE Add-PSModulePath -PathToAdd "C:\MyModules" Adds "C:\MyModules" to the PSModulePath if it's not already present. .EXAMPLE Add-PSModulePath -PathToAdd "D:\OtherModules" Adds "D:\OtherModules" to the PSModulePath if it's not already present. .NOTES Author: Viorel Ciucu Date: 2024-09-19 This function is useful for dynamically managing PowerShell module directories in your environment. #> function Add-PSModulePath { param ( [Parameter(Mandatory = $true)] [ValidateScript({ Test-Path $_ })] # Ensure the path exists before proceeding [string]$PathToAdd ) try { # Get the full path and remove trailing backslash $PathToAdd = (Get-Item -Path $PathToAdd | Select-Object -ExpandProperty FullName).TrimEnd('\') # Check if the path is already in $env:PSModulePath if (-not ($env:PSModulePath -split ';' | ForEach-Object { $_.TrimEnd('\') } | Where-Object { $_ -eq $PathToAdd })) { # If not, add it to the beginning of $env:PSModulePath $env:PSModulePath = "$PathToAdd;$env:PSModulePath" Write-Output "Path added to PSModulePath: $PathToAdd" } else { Write-Output "Path already exists in PSModulePath: $PathToAdd" } } catch { Write-Error "An error occurred: $_" } } |
You can use this in your $PROFILE.
Happy importing!
0 Comments.