Powershell - Environment Variable

Windows Powershell Menu

About

environment variable in powershell

Type

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

  • User = user profile.
  • Machine = computer as a whole,
  • Process = restricted to a single process.

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:

  • “User” makes TestVariable a user-level environment variable. Alternatively, we could have set this to “Machine” (machine-level) or “Process” (process-level).

Remove

  • The Remove-Item cmdlet
Remove-Item Env:\TestVariable
  • assigning the environment variable a null value
[Environment]::SetEnvironmentVariable("TestVariable",$null,"User")

Documentation / Reference







Share this page:
Follow us:
Task Runner