Windows用户环境变量与系统环境变量

thi*_*ter 12 windows powershell cmd environment-variables

在Windows中,是否有任何shell/Powershell命令分别列出用户环境变量和系统环境变量.如果我做 -

SET TEMP
Run Code Online (Sandbox Code Playgroud)

Windows为Temp显示用户环境变量而不是System变量.

我正在寻找shell命令/开关来分别显示这些变量.

Mik*_*ray 22

在PowerShell中,没有cmdlet,但您可以在Environment类中使用基础.NET方法:

Write-Host "Machine environment variables"
[Environment]::GetEnvironmentVariables("Machine")

Write-Host "User environment variables"
[Environment]::GetEnvironmentVariables("User")

# This should be the same as 'Get-ChildItem env:', although it isn't sorted.
Write-Host "Process environment variables"
[Environment]::GetEnvironmentVariables("Process")
Run Code Online (Sandbox Code Playgroud)

  • 要获取特定变量,请使用`[Environment] :: GetEnvironmentVariable("TEMP","Machine")` (3认同)