This blog has moved to http://ThePowerShellGuy.com
I had some problems creating Sytem-wide environment variables :Setting the Path / system variables from MSH
I found a workaround, Create System Variable From MSH
but also I got some Excelent answers from the NG (both better) :
One from Keith Hill :Take a look at .NET's System.Environment.SetEnvironmentVariable() methods.
One of the overloads takes an enum that lets you specify User or System
I did And yes :
this Works :
and a tip from Jeffrey Snover :MSH> $env = (new-object -com "WScript.Shell").Environment("SYSTEM")
MSH> ,$env gm
Notice the leading ",". The problem that you had was the $env is ENUMERABLE
so putting it into the pipeline unrolls it into its parts (strings), By
putting a "," in front you are creating a LIST of 2 elements
2) $ENV (the IENUMERABLE)
This list gets unrolled and passes on its parts ($ENV) so that GM reports
The less esoteric way to address this is:
MSH> gm -input $env
In any case, the Setter works:
MSH> $env.item("TMP") = "c:\temp"
and this one really got me, that little comma solves alot of the problems I was having exploring .NET collections etc. from MSH.
E.g. the problem I had while working with The DataTable :
see Strange behavour of get-member on DataTables
I will blog more about this later I think, but for now this works :
$dt = new-object system.data.dataTable