How can I display the contents of an environment variable from the command prompt in Windows 7?

1,994,800

Solution 1

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

To get a list of all environment variables enter the command set

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


Related

Solution 2

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

Solution 3

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%

Solution 4

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

Solution 5

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*

Share:
1,994,800

Related videos on Youtube

Jonas
Author by

Jonas

I'm a Computer Science student.

Updated on September 18, 2022

Comments

  • Jonas
    Jonas over 1 year

    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
      Jonas over 12 years
      @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.
    • Technophile
      Technophile over 6 years
    • Andry
      Andry almost 6 years
      echo %path:;=&echo.% gets the pretty list of semicolon separated paths. Works if a variable does not contain special characters like & or ^.
    • Vyacheslav Lanovets
      Vyacheslav Lanovets about 5 years
      the command to print path in Windows command shell is: path
  • Johnny_D
    Johnny_D over 10 years
    Why is this value different from what I've specified in computer properties?
  • Scott Chamberlain
    Scott Chamberlain over 10 years
    @Johnny_D It is likely that either you have a user scoped variable or you have a session scoped variable (using the set command inside a command prompt does not keep the change after you close the console window) that is overriding it.
  • Grzegorz Łuszczek
    Grzegorz Łuszczek over 8 years
    Also in PS: ls env: for listing all environment variables
  • Pacerier
    Pacerier about 7 years
    @RedGrittyBrick, How do you differentiate those set throughout the system vs those that are only for the current user?
  • user5249203
    user5249203 about 7 years
    @Pacerier, that would be a separate question
  • SikoSoft
    SikoSoft almost 6 years
    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.
  • Mad Physicist
    Mad Physicist almost 6 years
    @Lev. What version do you have that does not have cmd?
  • YakovL
    YakovL almost 5 years
    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)
  • Amit Naidu
    Amit Naidu about 3 years
    There is no $Env on cmd.exe. Just use the path command or echo %PATH%.