Windows PowerShell Tip of the Week

Here’s a quick tip on working with Windows PowerShell. These are published every week for as long as we can come up with new tips. If you have a tip you’d like us to share or a question about how to do something, let us know.

Find more tips in the Windows PowerShell Tip of the Week archive.

Creating and Modifying Environment Variables

Most people know how easy it is to use Windows PowerShell to retrieve information about environment variables. Want to see all your environment variables and their values? This command should do the trick:

Get-ChildItem Env:

In turn, you should get back information similar to this extract:

Name                           Value
----                           -----
Path                           D:\Perl\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\...
TEMP                           C:\DOCUME~1\kmyer\LOCALS~1\Temp
SESSIONNAME                    Console
PATHEXT                        .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PS1;.PSC1
USERDOMAIN                     WINGROUP
PROCESSOR_ARCHITECTURE         x86
SystemDrive                    C:
APPDATA                        C:\Documents and Settings\kmyer\Application Data
windir                         C:\WINDOWS
USERPROFILE                    C:\Documents and Settings\kmyer

Or, if you you’d like information about a particular environment variable you could use a command similar to this:

$Env:os

Pretty easy, and pretty useful, stuff indeed.

Note. You can also use the .NET Framework to return information about a particular environment variable:

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

In this case, we’re calling the GetEnvironmentVariable method, passing the method two parameters: Sample, the name of the environment variable we want to retrieve; and User, the type of environment variable. The User type is an environment variable tied to a user profile. You can also have Machine environment variables, which are tied to the computer as a whole, and Process environment variables, which are restricted to a single process.

That’s pretty cool. But what if you want to create a new environment variable, or maybe modify the value of an existing environment variable? What then?

Creating – and Modifying -- Environment Variables

There are several different ways to create new environment variables using Windows PowerShell. For example, suppose all you need is a process-level environment variable (that is, an environment variable that is only visible to, and lasts only as long as, your current PowerShell session). In that case you can create the new environment variable using code similar to this:

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

As you can see, we didn’t have to do much here. We simply specified a name for our new environment variable (TestVariable) using the syntax $env:TestVariable. We then assigned this environment variable the value 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. If it turns out that an environment variable named TestVariable already exists then PowerShell will simply assign the new value to the existing variable.

Which, as you might have guessed, is also one way you can change the value of an existing environment variable.

Like we said, though, that approach only works for process-level environment variables. To create more permanent environment variables (i.e., user-level or machine-level) you need to use the .NET Framework and the SetEnvironmentVariable method. For example, this command creates a user-level environment variable named TestVariable:

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

Here we’re using the .NET Framework’s System.Environment class and the SetEnvironmentVariable method. As you can see, we’re passing this method three parameters:

  • “TestVariable”, the name to be given to our new environment variable.

  • “Test value.”, the value to be assigned to the new environment variable.

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

Again, if TestVariable does not exist PowerShell will create the new environment variable for us. If TestVariable does exist, then PowerShell will assign it the value Test value.

One thing to watch out for: when we used SetEnvironmentVariable to create a new user- or machine-level environment variable that variable didn’t always show up when we ran this command in Windows PowerShell:

Get-ChildItem Env:

Or at least it didn’t show up until we restarted PowerShell. (Or started up a new instance of PowerShell.) However, we could retrieve the value of the new variable at any time by using this command:

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

Deleting Environment Variables

So how do you get rid of an environment variable after you no longer need it? Well, one way is to use the Remove-Item cmdlet, like so:

Remove-Item Env:\TestVariable

Alternatively you can use the SetEnvironmentVariable method, assigning the environment variable a null value:

[Environment]::SetEnvironmentVariable("TestVariable",$null,"User")

Again, if you use SetEnvironmentVariable the deleted variable might still show up when you call Get-ChildItem, at least until you restart. But take our word for it: the environment variable will no longer exist, as this command will demonstrate:

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