简体   繁体   English

在 powershell 中更改 [Environment]::GetEnvironmentVariables('Machine') 命令的输出或获取环境变量名称的列表

[英]Change the output of the [Environment]::GetEnvironmentVariables('Machine') command in powershell OR get the list of the names of the environment var

I'm trying to list the system environment variables in Powershell, and to change the output to something like:我试图在 Powershell 中列出系统环境变量,并将输出更改为如下内容:

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

So something with name=value and a return to the line after all variable所以一些带有 name=value 的东西,并在所有变量之后返回到该行

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:并尝试使用“Get-Member”来获取所有变量名称,但它没有显示我需要的内容:

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)在那种情况下,我想获取变量名称的列表以创建一个循环,该循环调用 $array[0].Name,其中 name 是变量的名称(我还没有编写代码,因为我无法得到它列表)

So my questions are:所以我的问题是:

How can I get the list of the environment variable names (TEMP, USERNAME, etc)?如何获取环境变量名称列表(TEMP、USERNAME 等)?

How can I change my output to make it looks like name=value?如何更改我的输出以使其看起来像名称=值?

Since GetEnvironmentVariables outputs IDictionary you can enumerate each Key / Value pair using the GetEnumerator Method :由于GetEnvironmentVariables输出IDictionary ,您可以使用GetEnumerator方法枚举每个键/值对:

[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:如果您只想列出一组特定的环境变量,您可以更改逻辑并使用GetEnvironmentVariable而不是针对特定的环境变量:

$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)?如何获取环境变量名称列表(TEMP、USERNAME 等)?

Same as with any other type implementing IDictionary , you can call it's .Keys property :与任何其他实现IDictionary的类型一样,您可以将其称为.Keys属性

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM