List all environment variables from the command line
Asked Answered
D

8

1180

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

Dupondius answered 16/3, 2011 at 15:22 Comment(1)
For a description of each, see ss64.com/nt/syntax-variables.htmlMandibular
E
1645

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
Evanthe answered 16/3, 2011 at 15:24 Comment(7)
This prompts me for a name?Mauriac
@KevinMeredith: All commands in the Windows shell are case insensitive.Evanthe
@Mauriac 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 hereUrsola
This doesn't seem to work with cmd /c. /e:on flag also doesn't help.Mazur
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.Ecumenicity
@John Lord: What is "command extensions"? Do you have a reference? Do you mean PowerShell Community Extensions (PSCX)?Swartz
i do not mean those. I'm talking about the command prompt (cmd.exe). Read this: 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.Ecumenicity
S
222

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

Straitlaced answered 10/7, 2013 at 15:26 Comment(1)
it's actually built in to some functions as well. Dir for example will page with a /p.Ecumenicity
G
176

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)

Grefe answered 17/2, 2015 at 8:3 Comment(3)
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 ...Taffeta
To avoid output being truncated, I would use the following: Get-ChildItem Env: | Format-Table -Wrap -AutoSizeHit
gci env: instead Get-ChildItem Env:, easier to rememberCannoneer
S
94

Simply run set from cmd.

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

Subdue answered 16/3, 2011 at 15:27 Comment(3)
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. `Amylum
set is run in cmd, not Powershell @JayKilleenDunning
So VERY very true @Grant! +1 But I think the MORE command option was to paginate. :D @JayKilleen and any other people: ---> Command Prompt CMD Answer: set | more (or in shortcut: set | more & pause) ---> PowerShell PS Answer: gci env: | Format-Table -Wrap -AutoSize -OR- Get-ChildItem Env: | Format-Table -Wrap -AutoSizeParma
T
25

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:
Torrell answered 16/7, 2021 at 17:13 Comment(2)
set by itself doesn't work with Github Actions, but the reg query does!Encephalomyelitis
Sorry @HrishikeshKadam - it doesn't work. Command: PS C:\Users\User> dir env Error Message: dir : Cannot find path 'C:\Users\User\env' because it does not exist... FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommandParma
T
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 /?.

Tiein answered 13/1, 2015 at 6:57 Comment(1)
How is this different from existing answers?Swartz
W
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.

Warford answered 6/2, 2017 at 13:0 Comment(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"Rudiger
Why do you say "don’t lose time"? Isn’t writing "set" in the command prompt faster than "reg query ..."?Exegesis
W
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)

Woody answered 5/12, 2016 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.