Modify and Create Environment Variables

In the past post PowerShell Environment Variables we talked about how to get the list of all the environment variables.  Now we are going to go over how to modify and create them.

Modify an Environment Variable

It helps to know what environment variables currently exist. Note that Microsoft and a lot of MSI and MSP files use the existing environment variables to help install or uninstall. Only modify at your own risk. If you work with Java it requires that you configure the path.

[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\Program Files\Java\jdk1.8.0\bin",  "<option>")

What you need to notice is $env:Path + “;” The $env:Path + tells it to add it on well ; tells it to end and start a new path.

Creating an Environment Variable

We can create a temporary variable that only last as long as your PowerShell session or we can create a permanent environment variable that is accessible anytime.

Creating a temporary variable is done like this:
$env:tempvariable = $env:LOGONSERVER + $env:HOMEPATH
\\FALLENHOOT\Users\Fallen Hoot

For this example I combined two environment variable to be used as one.

Now I am going to setup a permanent variable. Using this one-liner for a common environment variable for Java. The “<option>” parameter allows you to tell PowerShell the type of environment variable should be created. This has to deal with ExecutionPolicy. You read more about that here.

[Environment]::SetEnvironmentVariable("JAVA_HOME", "C:\Program Files\Java\jdk1.8.0",  "<option>")

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s