PowerShell: Environment Variables

List all environment variables:

Get-ChildItem env:
ls env:

Print an environment variable:

Get-ChildItem env:appdata
ls env:appdata

Create/set (permanent)

[Environment]::SetEnvironmentVariable("variable_name", "variable_value" ,"User")
[Environment]::SetEnvironmentVariable("variable_name", "variable_value" ,"Machine")
[Environment]::SetEnvironmentVariable("variable_name", "variable_value" ,"Process")

Unset (permanent)

[Environment]::SetEnvironmentVariable("variable_name", "" ,"User")
[Environment]::SetEnvironmentVariable("variable_name", "" ,"Machine")
[Environment]::SetEnvironmentVariable("variable_name", "" ,"Process")

Create/set (temporary):

$env:<variable-name>=100
$env:<variable-name>="C:\"

Unset (temporary):

$env:<variable-name>=""

Use an environment variable:

echo $env:<variable-name>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *