1152

Is it possible to list all environment variables from a Windows' command prompt?

Something equivalent to PowerShell's gci env: (or ls env: or dir env:).

bahrep
  • 29,961
  • 12
  • 103
  • 150
Nicola Cossu
  • 54,599
  • 15
  • 92
  • 98

9 Answers9

1619

Just do:

SET

You can also do SET prefix to see all variables with names starting with prefix.

For example, if you want to read only derbydb from the environment variables, do the following:

set derby 

...and you will get the following:

DERBY_HOME=c:\Users\amro-a\Desktop\db-derby-10.10.1.1-bin\db-derby-10.10.1.1-bin
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jon
  • 428,835
  • 81
  • 738
  • 806
  • 26
    This prompts me for a name? – CMCDragonkai Nov 14 '13 at 19:14
  • 57
    @KevinMeredith: All commands in the Windows shell are case insensitive. – Jon Nov 20 '13 at 16:13
  • 14
    @CMCDragonkai are you using powershell? It appears that it has hikacked set with one of its command-lets. This is one of its less useful features! I asked a question about disabling this [here](http://stackoverflow.com/questions/21678521/is-it-possible-to-how-do-you-stop-powershell-using-certain-cmdlets) – JonnyRaa Feb 27 '14 at 14:09
  • 4
    Can someone update this to include a PowerShell solution? It is supposed to be the future and all. Right? – Bruno Bronosky Dec 07 '16 at 16:28
  • This doesn't seem to work with `cmd /c`. `/e:on` flag also doesn't help. – arrowd Jul 04 '17 at 15:04
  • 1
    This is not guaranteed to work. This requires command extensions to be enabled. They are enabled by default on newer windows, but not older ones, and they can be turned off. – John Lord Dec 19 '18 at 17:30
  • @John Lord: What is "command extensions"? Do you have a reference? Do you mean [PowerShell Community Extensions](https://github.com/Pscx/Pscx) (PSCX)? – Peter Mortensen Sep 26 '19 at 08:43
  • i do not mean those. I'm talking about the command prompt (cmd.exe). Read this: https://www.computerhope.com/forum/index.php?topic=13988.0 They can break command scripts in some cases when enabled so it's possible a user that does a lot of batch processing has them turned off by default, and they can be turned of on a single-use instance as well in the batch file. In a specific command instance, it may be required to run this as well: C:> cmd.exe /E:ON to turn them on. – John Lord Sep 27 '19 at 16:21
  • 1
    These commands didn't work on Powershell but they are working well in CMD. Thanks! – Shrikant Kakani Oct 29 '21 at 19:18
  • @ShrikantKakani the question explicitly asks "how do you do the thing that is done with XYZ in Powershell", so that information is already there. – Jon Nov 01 '21 at 09:14
210

Jon has the right answer, but to elaborate a little more with some syntactic sugar..

SET | more

enables you to see the variables one page at a time, rather than the whole lot, or

SET > output.txt

sends the output to a file output.txt which you can open in Notepad or whatever...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fetchez la vache
  • 4,940
  • 4
  • 36
  • 55
162

To list all environment variables in PowerShell:

Get-ChildItem Env:

Or as suggested by user797717 to avoid output truncation:

Get-ChildItem Env: | Format-Table -Wrap -AutoSize

Source: Creating and Modifying Environment Variables (Windows PowerShell Tip of the Week)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user52028778
  • 27,164
  • 3
  • 36
  • 42
  • 4
    Even if I don't use PowerShell because it doesn't work for every cmd command, this is the only solution for a pretty printing (on 2 columns) without big efforts. To achieve the same behavior in cmd, you need something like this `for /f "tokens=1,2 delims==" ...` which becomes very complicated ... – ROMANIA_engineer Jan 14 '16 at 09:12
  • 6
    To avoid output being truncated, I would use the following: `Get-ChildItem Env: | Format-Table -Wrap -AutoSize` – user797717 Nov 13 '17 at 11:50
  • 5
    `gci env:` instead `Get-ChildItem Env:`, easier to remember – mati kepa Sep 26 '19 at 11:35
88

Simply run set from cmd.

Displays, sets, or removes environment variables. Used without parameters, set displays the current environment settings.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • 1
    Doesn't seem to work these days (in 2021) ` $ set $ cmdlet Set-Variable at command pipeline position 1 $ Supply values for the following parameters: $ Name[0]: $ Set-Variable: Cannot bind argument to parameter 'Name' because it is an empty array. ` – Jay Killeen Mar 02 '21 at 04:11
  • 2
    set is run in cmd, not Powershell @JayKilleen – Alistair Knock Dec 16 '21 at 16:28
23

Non expanded variables -

User variables -

reg query HKEY_CURRENT_USER\Environment

System variables -

reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"

Expanded variables -

In CMD -

SET

In Powershell -

Source - https://devblogs.microsoft.com/scripting/powertip-use-windows-powershell-to-display-all-environment-variables/

dir env:
Hrishikesh Kadam
  • 35,376
  • 3
  • 26
  • 36
14

You can use SET in cmd

To show the current variable, just SET is enough

To show certain variable such as 'PATH', use SET PATH.

For help, type set /?.

Boyce Field
  • 149
  • 1
  • 2
11

Don't lose time. Search for it in the registry:

reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"

returns less than the SET command.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paweł Piwowar
  • 174
  • 2
  • 8
  • 2
    although that might be true, one difference is that the registry query returns unexpanded `REG_EXPAND_SZ` keys. For example `reg query "HKCU\Environment"` shows me that my `%TEMP%` variable depends on the value of `%USERPROFILE%`, so if that value changes, so would the value for `%TEMP%`. In contrast, `SET` just returns `"C:\Users\mpag\AppData\Local\Temp"` – mpag Apr 27 '17 at 23:48
  • 6
    Why do you say "don’t lose time"? Isn’t writing "set" in the command prompt faster than "reg query ..."? – Alexandre Huat Mar 02 '20 at 13:42
9

As mentioned in other answers, you can use set to list all the environment variables or use

set [environment_variable] to get a specific variable with its value.

set [environment_variable]= can be used to remove a variable from the workspace.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
5

If you want to see the environment variable you just set, you need to open a new command window.

Variables set with setx variables are available in future command windows only, not in the current command window. (Setx, Examples)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
captain puget
  • 107
  • 1
  • 5