8

I am hoping there is a simple answer for this is. Is there a command that would give me a list of all User Environment variables? (or value of a user Environment variable if it exists). I know there is 'set' command, however it lists both user and system variables. I am running a script and I would like to know if a particular environment variable exists in user variables. Only other option would be using registry information. But I would like to know if this is possible with a commandline or something similar.

Thanks

Nik
  • 153
  • 1
  • 3
  • 12
  • Once the variables are in a process's environment, the information about where they came from are long gone. – Adrian McCarthy Nov 20 '15 at 23:44
  • near duplicate: http://stackoverflow.com/questions/30675480/windows-user-environment-variable-vs-system-environment-variable?rq=1 – Adrian McCarthy Nov 20 '15 at 23:45
  • 1
    @AdrianMcCarthy technically not really true, for example you can list the environment variables in C# separately for the process, user or machine targets: https://msdn.microsoft.com/en-us/library/system.environmentvariabletarget(v=vs.110).aspx – Igor Brejc Mar 22 '18 at 09:32
  • @Igor Brejc: I'm not sure that's really checking the same thing. I believe the enumeration is for examining and manipulating the variables that would be instantiated for a new process. If you create a process, then change a system environment variable, that process's environment will still have the old value. (That's my educated guess. I wasn't familiar with this .Net interface.) – Adrian McCarthy Mar 22 '18 at 18:53

4 Answers4

6

Querying the registry is probably the most simple method of returning only User environment variables:

reg query HKEY_CURRENT_USER\Environment

This is an example of what output is returned:

λ reg query HKEY_CURRENT_USER\Environment

HKEY_CURRENT_USER\Environment
    ChocolateyLastPathUpdate    REG_SZ    132148451031142736
    ChocolateyToolsLocation    REG_SZ    C:\tools
    ip    REG_SZ    ipconfig /all | findstr /IR "ipv4 ethernet adapter" | findstr /IRV "description tunnel vpn dial bluetooth [2-9]:$" | findstr /LV "*"
    Path    REG_EXPAND_SZ    C:\PATH\gnuwin32\bin;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;
    QB    REG_SZ    tasklist /FI "IMAGENAME eq qbittorrent.exe"
    TEMP    REG_EXPAND_SZ    %USERPROFILE%\AppData\Local\Temp
    TMP    REG_EXPAND_SZ    %USERPROFILE%\AppData\Local\Temp
Travis Runyard
  • 167
  • 2
  • 5
3

Yes, just use the set command :

SET

List all environment variables from command line?

If you want to see everything with a specified prefix you use :

SET prefix 

enter image description here

Community
  • 1
  • 1
Caffeinated
  • 11,982
  • 40
  • 122
  • 216
  • 6
    You missed this part of the question `I know there is 'set' command, **however it lists both user and system variables**`. – dxiv Nov 20 '15 at 19:33
  • 1
    @Coffee , SET command lists both user and system env variable. I need to know if there is **user** env variable exist. – Nik Nov 20 '15 at 19:43
1

1-the way to get variables in user environment in batch file:

(
    echo Set Sh = CreateObject^("WScript.Shell"^)
    echo Set User = Sh.Environment^("user"^)
    echo For Each strItem In User
    echo msgbox strItem
    echo Next
    ) > %tmp%\user.vbs
    start /wait %tmp%\user.vbs
    del /f %tmp%\user.vbs

2- in VBscript file:

  Set Sh = CreateObject( "WScript.Shell" )
    Set User = Sh.Environment( "user" )
    For Each strItem In User
        txt= strItem & vbCrlf & txt
    Next
    sh.popup txt,10,"list ofvariables in User environment"
hollopost
  • 569
  • 9
  • 28
0

Using psexec (https://technet.microsoft.com/en-us/sysinternals/psexec.aspx) at an elevated cmd prompt, the following will give you the list of system variables.

psexec -s cmd /c set

You could diff that list against the set list at a normal prompt to detect which variables are different i.e. are user variables.

I don't see a more direct way to do it (other than reading the environment from the registry).

dxiv
  • 16,984
  • 2
  • 27
  • 49
  • Based on the information in the link you supplied, it looks like I need to install Pstools and it doesn't come with Windows OS? It is strange that Windows has SETX command to set the variables in user or system level, but no command to get the variables user vs system. – Nik Nov 20 '15 at 20:07
  • @Nik You just need to expand the ZIP file, there is no installation required. The 1st time you run psexec it will pop a box with the terms that you have to OK, or you can run `psexec -accepteula` to bypass it. – dxiv Nov 20 '15 at 20:13
  • Thanks. I am running a script which will be running on user machine where I don't have any control on adding this exe. I think I will explore the registry option – Nik Nov 20 '15 at 20:18
  • Wish the late downvoter had at least left a comment why. – dxiv Jul 18 '18 at 15:25
  • 1
    ➕1 for mentioning psexec and pstools package! Kudos to Mark Russinovich of Sysinternals! :) – Dimitry K Nov 17 '20 at 15:03