Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows user environment variable vs. system environment variable

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 a shell command/switch to display these variables separately.

like image 956
thinkster Avatar asked Jun 05 '15 20:06

thinkster


People also ask

Do user environment variables override system environment variables?

A regular user environment variable overrides completely a system one with the same name if both exist, but only for the specific user it is specified for. However, the user path variables is treated differently. It is appended to the system path variable when evaluating, rather than completely replacing it.

What is a user environment variable?

User environment variables, as the name suggests, are environment variables that are specific to each user account. This means that the value of a variable when logged in as one user can be different than the value of the same variable when logged in as a different user on the same computer.

What are Windows system environment variables?

Environment variables store data that is used by the operating system and other programs. For example, the WINDIR environment variable contains the location of the Windows installation directory. Programs can query the value of this variable to determine where Windows operating system files are located.


2 Answers

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")
like image 57
Mike Zboray Avatar answered Oct 20 '22 20:10

Mike Zboray


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
like image 27
DavidPostill Avatar answered Oct 20 '22 21:10

DavidPostill