Quickly Setting Multiple Environment Variables

by Aug 21, 2015

To quickly (and permanently) set a bunch of environment variables, here is a nice approach:

$hashtable = @{
    Name = 'Weltner'
    ID = 12
    Ort = 'Hannover'
    Type = 'Notebook'
    ABC = 123
}

$hashtable.Keys | ForEach-Object {
    $Name = $_
    $Value = $hashtable.$Name
    [Environment]::SetEnvironmentVariable($Name, $Value, "User")

} 

Simply define the variables in a hash table. The script will create one environment variable per key-value pair. Replace "User" by "Machine" to create machine-wide environment variables. This would require Administrator privileges, though.

With the very same approach, you can also delete environment variables. Simply assign an empty string to it inside the hash table.

Twitter This Tip! ReTweet this Tip!