/config/powershell/psmodulepath
$env:PSModulePath: where PowerShell looks for modules and how to add a folder
- tool
- PowerShell
- file
- Environment variable PSModulePath
- key
- PSModulePath
- type
- list of folders separated by ; on Windows and : on Linux and macOS
- default
- user Modules folder; Program Files Modules folder; $PSHOME\Modules
$env:PSModulePath is the list of folders PowerShell searches when you run Import-Module Name and when it auto-loads a module because you used one of its cmdlets. A module is found when a folder in this list contains Name\Name.psd1 (or .psm1, .dll), optionally inside a version folder (Name\1.2.0\Name.psd1). To use modules from your own folder, add it to the variable.
$env:PSModulePath -split [IO.Path]::PathSeparatorAdd a folder
For the current session:
New-Item -ItemType Directory -Path MyModules\Greeting | Out-Null
Set-Content MyModules\Greeting\Greeting.psm1 'function Get-Greeting { "hello from Greeting" }'
"before: $(@(Get-Module -ListAvailable Greeting).Count) module(s) found"
$env:PSModulePath = (Join-Path $PWD 'MyModules') + [IO.Path]::PathSeparator + $env:PSModulePath
"after: $(@(Get-Module -ListAvailable Greeting).Count) module(s) found"
Get-Greeting
(Get-Module Greeting).Name + ' was auto-loaded'Output
before: 0 module(s) found after: 1 module(s) found hello from Greeting Greeting was auto-loaded
Get-Greeting works without Import-Module: PowerShell auto-loads modules that are on the path.
To keep the folder for every session, add the same line to your profile:
$env:PSModulePath = 'D:\PowerShell\Modules' + [IO.Path]::PathSeparator + $env:PSModulePathScripts started with -NoProfile (scheduled tasks, CI) do not read the profile; set the variable in the script itself, or install the module into one of the default folders below.
Default folders
| Scope | Windows PowerShell 5.1 | PowerShell 7 |
|---|---|---|
| Current user | Documents\WindowsPowerShell\Modules | Documents\PowerShell\Modules |
| All users | $env:ProgramFiles\WindowsPowerShell\Modules | $env:ProgramFiles\PowerShell\Modules |
| Built-in | $PSHOME\Modules | $PSHOME\Modules |
Install-Module -Scope CurrentUser installs into the current-user folder, -Scope AllUsers (admin) into Program Files. PowerShell 7 on Windows also appends the Windows PowerShell folders to its path, so it can find and load many 5.1 modules; Windows PowerShell does not see modules installed only for PowerShell 7.
Sources
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_psmodulepath
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_modules
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/import-module