Kyle Stoflet
Kyle Stoflet

Reputation: 1234

How can I view my SYSTEM environment variables in a command prompt?

I've seen in multiple stack overflow posts that LOCAL path variables can be seen by using:

echo %Path%

But I'd like to view my SYSTEM path variables found in (Windows 10):

Control Panel > System and Security > System > Advanced system settings > Environment Variables... > System variables

Does anyone know how to view these from the command line? If this has already been answered for system variables, please point me in that direction.

EDIT: I'm running a line that checks to see if a variable exists, if it doesn't, I am resetting the value of Path to all of the old variables, plus the new one. I need only the system variables and no other variables, because when I store my current variables, plus the new one, I don't want any other variables to be appended that shouldn't belong in my system environment variables.

e.x. if I were to use echo %Path% or set %Path% I might be storing local variables in my system variables. I'd rather not do that.

Upvotes: 3

Views: 18506

Answers (3)

gk_2000
gk_2000

Reputation: 351

There is a way if we employ PowerShell. It can be done this way. (Corrections are welcome, but this should be pretty accurate.)
Note: You need to use this in administrator window.

  1. Capture current system path into a file like syspath.

    powershell "[System.Environment]::GetEnvironmentVariable('PATH',[System.EnvironmentVariableTarget]::Machine)" >syspath
    
  2. Get the system path into a variable.

    set /p v=<syspath
    
  3. Make new path from current syspath.

    set new_script_loc=C:\newscripts
    set newpath="%new_script_loc%;%v%"
    powershell "[System.Environment]::SetEnvironmentVariable('PATH','%newpath%',[System.EnvironmentVariableTarget]::Machine)"
    

Upvotes: 0

Konrad
Konrad

Reputation: 371

You can use reg.exe for that. Just displaying all system paths:

reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v path

When you want to display each path at one line here is a little batch:

@echo off
setlocal
for /f "tokens=2* delims= " %%d in ('REG query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PATH 2^>nul') do (
  set "_REMAIN_=%%~e"
  call :Sub1
)
exit /b 0

:Sub1
for /f "tokens=1* delims=;" %%d in ("%_REMAIN_%") do (
  echo %%~d
  set "_REMAIN_=%%~e"
  if not "%%~e"=="" call :Sub1
)
exit /b 0

Upvotes: 4

user6017774
user6017774

Reputation:

This lists the four types of variables. To use in a console

cscript //nologo C:\pathto\script.vbs

Note there are variables that aren't listed, these are listed in help for set - type set /?.

Set WshShell = CreateObject("WScript.Shell")


Set wshsysEnv = WshShell.Environment("SYSTEM")
Wscript.echo "System"
For Each S In wshsysEnv
    WScript.echo S 
Next
Wscript.echo ""

Set wshsysEnv = WshShell.Environment("Volatile")
Wscript.echo "Volatile"
For Each S In wshsysEnv
    WScript.echo S 
Next
Wscript.echo ""

Set wshsysEnv = WshShell.Environment("User")
Wscript.echo "User"
For Each S In wshsysEnv
    WScript.echo S 
Next
Wscript.echo ""

Set wshsysEnv = WshShell.Environment("Process")
Wscript.echo "Process"
For Each S In wshsysEnv
    WScript.echo S 
Next
Wscript.echo ""

To get the two paths (user path is blank on a new installation of windows, but software may change it)

Set wshsysEnv = WshShell.Environment("User")
Wscript.echo "User"
For Each S In wshsysEnv
    If lcase(left(S,5)) = LCase("PATH=") then WScript.echo S 
Next
Wscript.echo ""

Set wshsysEnv = WshShell.Environment("System")
Wscript.echo "System"
For Each S In wshsysEnv
    If lcase(left(S,5)) = LCase("PATH=") then WScript.echo S 
Next
Wscript.echo ""

To get just the system paths without "Path=" at the beginning, use this

Set WshShell = CreateObject("WScript.Shell")

Set wshsysEnv = WshShell.Environment("System")
For Each S In wshsysEnv
    If lcase(left(S,5)) = LCase("PATH=") then WScript.echo right(S,Len(S)-5)
Next
Wscript.echo ""

Upvotes: 5

Related Questions