784

In Windows 7, when I start the Command prompt, is there any command to display the contents of an environment variable (such as the JAVA_HOME or PATH variables)?

I have tried with echo $PATH, echo PATH and $PATH but none of these work.

Jonas
  • 27,554
  • 30
    @Daniel: I know how to set environment variables in Windows, I simply open "System properties" > "Advanced" and "Environment Variables". So I don't expect the answer to my question in a question titled with "How do I set PATH and other environment variables?", because I know that! I'm not asking about how to set them. – Jonas Oct 01 '11 at 12:00
  • Perhaps relevant: https://stackoverflow.com/questions/1884071/windows-echo-command-cant-echo-a-user-set-variable – Technophile Oct 30 '17 at 21:51
  • 6
    echo %path:;=&echo.% gets the pretty list of semicolon separated paths. Works if a variable does not contain special characters like & or ^. – Andry Jul 05 '18 at 17:22
  • 1
    the command to print path in Windows command shell is: path – Vyacheslav Lanovets May 02 '19 at 05:46

8 Answers8

840

In Windows Command-Prompt the syntax is echo %PATH%

To get a list of all environment variables enter the command set without any parameters.

To send those variables to a text file enter the command set > filename.txt


Related

285

To complement the previous answer, if you're using Powershell echo %PATH% would not work. You need to use the following command instead: echo $Env:PATH

Gerard Yin
  • 3,095
  • 46
    Also in PS: ls env: for listing all environment variables – George Mauer Aug 08 '15 at 16:24
  • 4
    Since PowerShell is now the default shell in modern Windows OS's this needs to be up-voted higher. Way too many answers out there that simply no longer work on modern Windows. – SikoSoft May 14 '18 at 10:35
  • 3
    @Lev. What version do you have that does not have cmd? – Mad Physicist May 24 '18 at 18:20
  • 1
    so how one shows a variable if its name contains a dot? Like artifactory.user.name? echo $Env:artifactory.user.name doesn't work (highlighting suggests that it tries to show the artifactory variable) – YakovL May 15 '19 at 14:52
  • i just wish to know WHY.. why it is still windows but yet every shell is not compatible with others. Is there a reason? i really want to know – DinoSaadeh Mar 07 '24 at 15:03
  • 1
    @YakovL ${env:artifactory.user.name} – zanedp Mar 12 '24 at 19:19
40

As an additional bit of information: While SET works with global or system variables, sometimes you want to write and read User variables, and this is done with the SETX command. SETX is included in the base installs of Windows beginning with Vista, but was also available in Windows XP by installing the Resource Pack.

One difference about SETX though is that you cannot read the variable out in the same command window you wrote it in. You have to write the SETX command in one Command or Powershell window, and then open a new window to read it using ECHO.

SETX can also write global or system variables.

To Set a user variable using SETX:

setx variable value

To set a global or system variable using SETX:

setx /m variable value

To read a user or global variable:

Remember, you must open a new Command or Powershell window to read this variable.

echo %variable%
music2myear
  • 41,771
16

From SET /?:

SET P

would display all variables that begin with the letter 'P'

So for example if you want to find value of environment variable %PATH%, you can just type set path.

This is 3 characters shorter than echo %PATH%, but note that it also lists other variables starting with "path" (e.g. PATHEXT).

AXO
  • 996
10

Powershell:

echo $Env:PATH

Command Prompt:

echo $Env:%PATH%

on the Command Prompt %PATH% will also work

9

To display contents of an environment variable eg. path, at command prompt type: echo %path%
To display the values in separate lines, type: set
To display all variables starting with "h", type: set h
(Press enter after typing to get computer response, duh!)

Above commands are for cmd, not powershell. In powershell, type: echo $env:path or ls env:path
To display on separate lines, type: ls env:
To display all variables starting with "h", type: ls env:h*
To display contents/values of all variables containing "java", type: ls env:*java*

Zimba
  • 1,131
6

On powershell if you want to list all the values splitter by semicolon delimiter, then use:

$env:Path-split';'
Wasif
  • 8,474
6

The solution was a bit different for me: it won't recognize the system environment variable JAVA_HOME, so I had to set JAVA_HOME as User environment variable, so that i can use %JAVA_HOME% in system environment variable setting up.

Resuming, I had to:

  • add a user environment variable: %JAVA_HOME% as:

    "C:\Program Files\Java\jdk1.8.0_25";
    
  • add to %PATH% system environment variable:

    "%JAVA_HOME%\bin;"
    
  • latter on command line:

    echo %JAVA_HOME%, and it retrieved the correct path (before it wasn't recognizing);

    echo %PATH%, and it retrieved the "C:\Program Files\Java\jdk1.8.0_25\bin" composed with %JAVA_HOME% user variable;

And it worked for me. I hope it helps!!

phuclv
  • 27,773
JoaoPT
  • 61