The $env:PSModulePath
environment variable contains a list of directory locations that PowerShell
searches to locate modules.
$env:PSModulePath
When you’re trying to determine what paths are part of the $env:PSModulePath
environment variable
in PowerShell, you’ll find that most examples split the results on the semicolon (;)
character to
return each path on a separate line.
$env:PSModulePath -split ';'
Another variation is to use the Split
method instead of the operator.
$env:PSModulePath.Split(';')
Splitting on a semicolon was a great solution back in the day of Windows (only) PowerShell. The
cross-platform GA release of PowerShell in 2018 made using the semicolon to split paths obsolete
because Linux and macOS use a colon (:)
as a path separator.
For the following examples, I’ve established a PowerShell remoting session to Windows, Linux, and
macOS systems and stored those sessions in a variable named PSSession
. I’ve also created a
function called uname
on the Windows system.
Notice that each path is on a separate line for the Windows system when splitting the
$env:PSModulePath
using a semicolon, but the paths for both Linux and macOS are on a single
wrapped line.
Invoke-Command -Session $PSSession {. uname; $env:PSModulePath -split ';'}
Splitting the $env:PSModulePath
with a colon solves the problem for Linux and macOS, but mangles
the paths for Windows.
Invoke-Command -Session $PSSession {. uname; $env:PSModulePath -split ':'}
A tip I received from Andrew Pearce is
that “[System.IO.Path]::PathSeparator
is your friend”.
[System.IO.Path]::PathSeparator
Splitting the $env:PSModulePath
using [System.IO.Path]::PathSeparator
solves the problem for
Windows, Linux, and macOS.
Invoke-Command -Session $PSSession {. uname; $env:PSModulePath -split [System.IO.Path]::PathSeparator}
Summary
Don’t use a semicolon to split the $env:PSModulePath
in a cross-platform world. Use
[System.IO.Path]::PathSeparator
instead.
$env:PSModulePath -split [System.IO.Path]::PathSeparator