fbpx View and Modify Environment Variables in PowerShell | Windows PowerShell, Windows Server | HostingUltraso.com
24/7/365 Support

View and Modify Environment Variables in PowerShell

Problem

You want to interact with your system’s environment variables.

Solution

To interact with environment variables, access them in almost the same way that you access regular PowerShell variables. The only difference is that you place env: between the ($) dollar sign and the variable name:

PS >$env:Username Lee

You can modify environment variables this way, too. For example, to temporarily add the current directory to the path:

PS >InvokeDemonstrationScript The term 'InvokeDemonstrationScript' is not recognized as a cmdlet, funct ion, operable program, or script file. Verify the term and try again. At line:1 char:26

+ InvokeDemonstrationScript <<<< PS >$env:PATH = $env:PATH + ";." PS >InvokeDemonstrationScript.ps1 The script ran!

Discussion

In batch files, environment variables are the primary way to store temporary information, or to transfer information between batch files. PowerShell variables and script parameters are more effective ways to solve those problems, but environment variables continue to provide a useful way to access common system settings, such as the system’s path, temporary directory, domain name, username, and more.

PowerShell surfaces environment variables through its environment provider—a container that lets you work with environment variables much like you would work with items in the filesystem or registry providers. By default, PowerShell defines an env: (much like the c: or d:) that provides access to this information:

PS >dir env:

Name
Value

Path
c:\progra~1\ruby\bin;C:\WINDOWS\system32;C:\

TEMP
C:\DOCUME~1\Lee\LOCALS~1\Temp

SESSIONNAME
Console

PATHEXT
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;

(...)

Since it is a regular PowerShell drive, the full way to get the value of an environment variable looks like this:

PS >GetContent Env:\Username Lee

When it comes to environment variables, though, that is a syntax you will almost never need to use, because of PowerShell’s support for the GetContent and SetContent variable syntax, which shortens that to:

PS >$env:Username Lee

This syntax works for all drives but is used most commonly to access environment variables.

Some environment variables actually get their values from a combination of two places: the machinewide settings and the currentuser settings. If you want to access environment variable values specifically configured at the machine or user level, use the [Environment]::GetEnvironmentVariable() method. For example, if you've defined a tools directory in your path, you might see:

PS >[Environment]::GetEnvironmentVariable("Path", "User") d:\lee\tools

To set these machine or userspecific environment variables permanently, use the [Environment]::SetEnvironmentVariable() method:

[Environment]::SetEnvironmentVariable(<name>, <value>, <target>)

The Target parameter defines where this variable should be stored: User for the current user, and Machine for all users on the machine. For example, to permanently add your Tools directory to your path:

PS >$oldPersonalPath = [Environment]::GetEnvironmentVariable("Path", "User") PS >$oldPersonalPath += ";d:\tools" PS >[Environment]::SetEnvironmentVariable("Path", $oldPersonalPath, "User")

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say