How do I set the PATH in PowerShell?

The PATH, like all environment variables, is accessible in PowerShell via the Env drive, and you can set it using variable syntax, like: $Env:PATH += ";$pwd" to add the present working directory to it. In Windows, the directory separator for the path is ";", but on Linux it's ":" -- to be safe, you can use [IO.Path]::PathSeparator like this: $Env:Path += [IO.Path]::PathSeparator + $pwd
Note that += is the "append" operator for strings.
To make changes permanent you can either put them in a profile script (which is cross-platform), or if you need the changes to affect apps outside PowerShell on Windows, you can force them into the environment storage using [Environment]::SetEnvironmentVariable
 

function Add-Path($Path) {
$Path = [Environment]::GetEnvironmentVariable("PATH", "Machine") + [IO.Path]::PathSeparator + $Path
[Environment]::SetEnvironmentVariable( "Path", $Path, "Machine" )
}

posted @ 2023-02-10 11:50  huorexiaji  阅读(24)  评论(0)    收藏  举报