If sometime during a PowerShell session you need to see or to temporarily modify the PATH environment variable, you can type one of these commands:
Copy$env:Path # shows the actual content
$env:Path = 'C:\foo;' + $env:Path # attach to the beginning
$env:Path += ';C:\foo' # attach to the end
Answer from mloskot on Stack OverflowIf sometime during a PowerShell session you need to see or to temporarily modify the PATH environment variable, you can type one of these commands:
Copy$env:Path # shows the actual content
$env:Path = 'C:\foo;' + $env:Path # attach to the beginning
$env:Path += ';C:\foo' # attach to the end
Changing the actual environment variables can be done by
using the env: namespace / drive information. For example, this
code will update the path environment variable:
Copy$env:PATH = "SomeRandomPath"; (replaces existing path)
$env:PATH += ";SomeRandomPath" (appends to existing path)
Making change permanent
There are ways to make environment settings permanent, but if you are only using them from PowerShell, it's probably a lot better to use Powershell profiles script.
Everytime a new instance of Powershell starts, it look for specific script files (named profile files) and execute them if they do exist. You can edit one of these profile to customize your enviroment.
To know where those profile scripts are located in your computer type:
Copy$profile
$profile.AllUsersAllHosts
$profile.AllUsersCurrentHost
$profile.CurrentUserAllHosts
$profile.CurrentUserCurrentHost
You can edit one of them, for example, by typing:
Copynotepad $profile
How can I get and change environment variables' value in Windows Powershell
Change Environment Path and MAKE IT STICK
env and environment variables in Powershell
Adding a path to environment variable
How to set an environment variable using PowerShell?
To set an environment variable, you can use the following syntax.
$Env:VariableName = “Value”
Replace VariableName with the desired name of your variable and Value with the value you want to assign to it, as example below.
$Env:MY_VAR = “MyValue”
How do I list all variables in PowerShell?
To list all variables in your PowerShell session, you can use:
Get-ChildItem Env:
This command will show all currently defined variables in the session, including environment variables.
How to check if an environment variable exists in PowerShell?
To check if a specific environment variable exists, you can use the following command.
Test-Path Env:MY_VAR Replace MY_VAR with the name of the environment variable you want to check. If the variable exists, it will return True; if not, it will return False.
Videos
Hi all,
We've got an odd issue where random machines (all Win11) cannot run Winget, even though it's installed. I've identified the cause as being Winget isn't included in the PATH environment variable. Now I've got a script written for this (as an Intune Remediation), but in testing this won't stick.
Found an article about setting this to the Machine context, but not sure if I'm doing it right because it still won't goddamned stick. Script below - can anyone assist with this?
# Get winget path into variable
$wingetPath = Resolve-Path "C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe"
# Extract PATH into separate values
$pathParts = $env:PATH -split ';'
# Append winget path to PATH values
$addToPath = $pathParts + $wingetPath | Where-Object { $_ }
# Reconstitute and set PATH with new variables
$newEnvPath = $addToPath -join ';'
[System.Environment]::SetEnvironmentVariable('PATH',$newEnvPath)