Windows User Environment Variable vs System Enviro

2019-04-08 10:14发布

In Windows, is there any shell/Powershell command to list User Environment Variable and System Environment Variable separately. If I do -

SET TEMP

Windows displays the User Environment Variable instead of System variable for Temp.

I am looking for shell command/switch to display these variables separately.

2条回答
甜甜的少女心
2楼-- · 2019-04-08 10:48

Use the following batch file:

@echo off
for /f "tokens=3 usebackq" %%a in (`reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" ^| findstr TEMP`)  do @echo System variable TEMP = %%a
for /f "tokens=3 usebackq" %%a in (`reg query "HKEY_CURRENT_USER\Environment" ^| findstr TEMP`)  do @echo Current user variable TEMP = %%a

To use from a command line replace %% with %.

Output:

System variable TEMP = %SystemRoot%\TEMP
Current user variable TEMP = %USERPROFILE%\AppData\Local\Temp

Note that the HKEY_CURRENT_USER takes precedance (but for some reason %USERPROFILE% is expanded to a shortname when evaluating %TEMP%):

echo %USERPROFILE%
USERPROFILE=C:\Users\DavidPostill

echo %TEMP%
C:\Users\DAVIDP~1\AppData\Local\Temp
查看更多
迷人小祖宗
3楼-- · 2019-04-08 11:03

In PowerShell, there's no cmdlet for it, but you can use the underlying .NET methods in the Environment class:

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")
查看更多
登录 后发表回答