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
Videos
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.
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.