Home > Enterprise >  Change the output of the [Environment]::GetEnvironmentVariables('Machine') command in powe
Change the output of the [Environment]::GetEnvironmentVariables('Machine') command in powe

Time:12-22

I'm trying to list the system environment variables in Powershell, and to change the output to something like :

NAME=VALUE
NUMBER_OF_PROCESSORS=8
TMP=C:\WINDOWS\TEMP

So something with name=value and a return to the line after all variable

The problem is that I can't make this output, the only thing I can found is :

Name  : NUMBER_OF_PROCESSORS
Value : 8

Name  : windir
Value : C:\WINDOWS

using the command :

[Environment]::GetEnvironmentVariables('Machine') | Format-List

This first one is almost what I want but it's still not perfect.

Or I can also have :

Name                           Value
----                           -----
NUMBER_OF_PROCESSORS           8
TMP                            C:\WINDOWS\TEMP
windir                         C:\WINDOWS

using the command :

[Environment]::GetEnvironmentVariables('Machine') | Format-Table

Which is badder than the first one because the line are cut (the path is not fully displayed because its to large to be printed on the screen)

I tried to put the output in a variable and only get the name of the variables but it still doesn't work :

PS> $array=[Environment]::GetEnvironmentVariables('Machine')
PS> $array[0].Name

and also tried "Get-Member" to get all the variable names but it don't show what I need :

PS> $array[0] | Get-Member

   TypeName : System.Collections.Hashtable

Name              MemberType            Definition
----              ----------            ----------
Add               Method                void Add(System.Object key, System.Object value), void IDiction...
Clear             Method                void Clear(), void IDictionary.Clear()
Clone             Method                System.Object Clone(), System.Object ICloneable.Clone()
Contains          Method                bool Contains(System.Object key), bool IDictionary.Contains(Sys...
ContainsKey       Method                bool ContainsKey(System.Object key)
ContainsValue     Method                bool ContainsValue(System.Object value)
CopyTo            Method                void CopyTo(array array, int arrayIndex), void ICollection.Copy...
Equals            Method                bool Equals(System.Object obj)
GetEnumerator     Method                System.Collections.IDictionaryEnumerator GetEnumerator(), Syste...
GetHashCode       Method                int GetHashCode()
GetObjectData     Method                void GetObjectData(System.Runtime.Serialization.SerializationIn...
GetType           Method                type GetType()
OnDeserialization Method                void OnDeserialization(System.Object sender), void IDeserializa...
Remove            Method                void Remove(System.Object key), void IDictionary.Remove(System....
ToString          Method                string ToString()
Item              ParameterizedProperty System.Object Item(System.Object key) {get;set;}
Count             Property              int Count {get;}
IsFixedSize       Property              bool IsFixedSize {get;}
IsReadOnly        Property              bool IsReadOnly {get;}
IsSynchronized    Property              bool IsSynchronized {get;}
Keys              Property              System.Collections.ICollection Keys {get;}
SyncRoot          Property              System.Object SyncRoot {get;}
Values            Property              System.Collections.ICollection Values {get;}

I also found that if you type :

$array[0].TMP

It shows you the value of the variable which is in my case :

C:\WINDOWS\TEMP

In that case I want to get the list of the variable name to make a loop which call $array[0].Name, where name is the name of the variable (and i did not code it yet because I can't get this list)

So my questions are :

How can I get the list of the environment variable names (TEMP, USERNAME, etc) ?

How can I change my output to make it looks like name=value ?

CodePudding user response:

Since GetEnvironmentVariables outputs IDictionary you can enumerate each Key / Value pair using the GetEnumerator Method:

[System.Environment]::GetEnvironmentVariables('Machine').GetEnumerator() | ForEach-Object {
    '{0}={1}' -f $_.Key, $_.Value
}

If you would like to list only a specific set of Environment Variables you could change the logic and use GetEnvironmentVariable instead to target specific ones:

$variablesOfInterest = 'TEMP', 'USERNAME'
$variablesOfInterest | ForEach-Object {
    '{0}={1}' -f $_, [System.Environment]::GetEnvironmentVariable($_)
}

Regarding:

How can I get the list of the environment variable names (TEMP, USERNAME, etc) ?

Same as with any other type implementing IDictionary, you can call it's .Keys property:

[System.Environment]::GetEnvironmentVariables('Machine').Keys
  • Related