3

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.

4
  • 1
    set ........? won't differentiate between system and user vars, but at least you'll get all the vars. Commented Jun 8, 2016 at 21:25
  • just try it. set by itself spits out the vars Commented Jun 8, 2016 at 21:26
  • Those are only stored in the registry, as far as I know. There's no documented solution to this. You can use reg query from the command prompt to read registry values. Commented Jun 8, 2016 at 21:28
  • Made an edit explaining how I'm going to use these printed variables, and why I need to only have system variables. Commented Jun 8, 2016 at 21:39

3 Answers 3

5

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 ""
Sign up to request clarification or add additional context in comments.

2 Comments

So I ended up getting this to run, I made the edit just before you posted, but I need only the path section of the system variables. I'm unfamiliar with the language, would you by chance know how to just print off that part? That'd fully satisfy what I was hoping to get with my question.
setx /m path c:\somepath add somepath to the system path. It will only add if it's not already in the path. It is available in future console windows. Set only applies changes to the current console - the current console's path is built from system, users, and c:\autoexec.bat (for compatibility) neither system or user variables but the program's variables. All paths in above locations are appended.
4

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

3 Comments

I've decided to use this as the answer as it doesn't require a vbscript as the other answer does. Therefore I can throw this into my current batch file and use less lines of code. Thanks!
But @KyleStoflet this is not the "correct" answer, as it doesn't print the sys environment, just the PATH.
This will give you all SYSTEM vars with some cleanup required: ``` reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"```
0

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)"
    

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.