ChatGPT解决这个技术问题 Extra ChatGPT

List all environment variables from the command line

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

For a description of each, see ss64.com/nt/syntax-variables.html

P
Peter Mortensen

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

This prompts me for a name?
@KevinMeredith: All commands in the Windows shell are case insensitive.
@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
Can someone update this to include a PowerShell solution? It is supposed to be the future and all. Right?
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.
P
Peter Mortensen

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


i had no idea more existed on Windows, +1
it's actually built in to some functions as well. Dir for example will page with a /p.
P
Peter Mortensen

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)


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 ...
To avoid output being truncated, I would use the following: Get-ChildItem Env: | Format-Table -Wrap -AutoSize
gci env: instead Get-ChildItem Env:, easier to remember
Perfect! I do not see why this isn't the top-answer to be honest :/ EDIT: Oh, wait, I see why... the original question wasn't about how to do it in Powershell, but in the old-school Commandprompt :D
G
Grant Thomas

Simply run set from cmd.

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


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. `
set is run in cmd, not Powershell @JayKilleen
B
Boyce Field

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


How is this different from existing answers?
H
Hrishikesh Kadam

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:

set by itself doesn't work with Github Actions, but the reg query does!
P
Peter Mortensen

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.


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"
Why do you say "don’t lose time"? Isn’t writing "set" in the command prompt faster than "reg query ..."?
P
Peter Mortensen

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.


Yes, but how does this answer the question?
P
Peter Mortensen

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)