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:
$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:
$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:
$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:
$profile
$profile.AllUsersAllHosts
$profile.AllUsersCurrentHost
$profile.CurrentUserAllHosts
$profile.CurrentUserCurrentHost
You can edit one of them, for example, by typing:
notepad $profile
Change Environment Path and MAKE IT STICK
How can I get and change environment variables' value in Windows Powershell
Set a persistent environment variable on a windows 10 computer with PowerShell?
env and environment variables in Powershell
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)
Note: After seeing lots of comments about setting environment variables without administrator rights in Windows 10, I think I have found a way. I was not administrator and could use PowerShell.
PowerShell method
You can list all environment variables with: Get-ChildItem Env:.
To get the value of a specific variable: $Env:PATH, where PATH is the name of the variable.
To set a variable: [Environment]::SetEnvironmentVariable("PATH", "C:\TestPath", "User"), the first parameter is the name of the variable, the second is the value, the third is the level of.
There are different ways to work with environment variables and certain quirks with them in PowerShell so consult the link for details.
Old method (no longer available in newer Windows 10 updates, use PowerShell or see other answers)
Go into Settings and click on System.

Then on the left side click About and select System info at the bottom.

In the new Control Panel window that opens, click Advanced system settings on the left.

Now in the new window that comes up, select Environment Variables... at the bottom.

Still the same as ever: It’s in the old-style control panel’s “System” thingy. You can reach it with WinBreak or by right-clicking the Start button.
From there, select “Advanced system settings” → “Environment Variables”.
Or you can do it the hard way and find some other entry point to the old-style control panel, like the Network and Sharing Center or the Desktop folder(!).
I need to set a system wide environment variable and I need it to be persistent.
Every time I try setting it with PowerShell it seems to only be good for that PowerShell session.
Is there a way to do this with PowerShell?