.net, Powershell

Assigning an Environment variable using Powershell

As part of deploying my applications to web servers (and other types of servers too), I like to hold certain values as environment variables. It’s possible to add these variables through the standard Windows UI, but I prefer to use command line tools to do this.

I can add environment variables to my own user profile very simply using a normal Powershell prompt.

[Environment]::SetEnvironmentVariable("VARIABLE_NAME", "localhost", "User")

And if I want to add an environment variable at a machine level, I can open a Powershell prompt as Administrator and use the command below:

[Environment]::SetEnvironmentVariable("VARIABLE_NAME", "localhost", "Machine")

This technique is particularly useful while automating the creation of infrastructure.

There’s more information about this here on TechNet.