Table of Contents

Powershell - Environment Variable

About

environment variable in powershell

Type

Beside the session scope, an environment variable may be stored with the following scopes:

Management

Display all variables

Get-ChildItem Env:
# or
dir env:

Show the variables that were set permanently before the session and not during the session

Display One

$Env:os
Windows_NT

Get

the .NET Framework to return information about a particular environment variable:

[Environment]::GetEnvironmentVariable("Sample","User")

where:

Set

Create and/or set

Session-level

$env:TestVariable = "This is a test environment variable."

If PowerShell can’t find an environment variable named TestVariable it will automatically create the variable and assign it the specified value.

User-level or machine-level

You need to use the .NET Framework and the SetEnvironmentVariable method.

[Environment]::SetEnvironmentVariable("TestVariable", "Test value.", "User")

where:

Remove

Remove-Item Env:\TestVariable
[Environment]::SetEnvironmentVariable("TestVariable",$null,"User")

Documentation / Reference