Windows命令提示符中如何列出全部环境变量?
Great question! You’ve hit on a key quirk of Windows Command Prompt: the standard set command only displays persisted system/user environment variables stored in the process environment block. It doesn’t show the dynamic "pseudo-variables" that cmd.exe manages internally (like %CD%, %DATE%, %ERRORLEVEL%) because these aren’t true environment variables—they’re runtime values generated by the shell itself.
Here are two reliable ways to get a full list of all variables (both real and pseudo):
1. Combine set with Manual Pseudo-Variable Output (Cmd Only)
If you need to stay within Command Prompt, you can create a simple batch script that merges the output of set with explicit calls to common pseudo-variables. This covers most dynamic values you might care about:
@echo off REM List standard environment variables echo === Standard Environment Variables === set echo. REM List cmd.exe pseudo-variables echo === Cmd Pseudo-Variables === echo CD=%CD% echo DATE=%DATE% echo TIME=%TIME% echo ERRORLEVEL=%ERRORLEVEL% echo RANDOM=%RANDOM% echo CMDCMDLINE=%CMDCMDLINE% echo CMDEXTVERSION=%CMDEXTVERSION% echo NUMBER_OF_PROCESSORS=%NUMBER_OF_PROCESSORS% echo OS=%OS% echo PATHEXT=%PATHEXT% echo PROMPT=%PROMPT% echo SYSTEMDRIVE=%SYSTEMDRIVE% echo SYSTEMROOT=%SYSTEMROOT% echo USERDOMAIN=%USERDOMAIN% echo USERNAME=%USERNAME% echo USERPROFILE=%USERPROFILE%
Save this as list-all-vars.bat, run it in cmd, and you’ll get a combined list of both types of variables.
2. Use PowerShell for a More Comprehensive View
PowerShell makes this much easier, as it can natively access both environment variables and its own set of automatic variables (which cover the same use cases as cmd’s pseudo-variables):
- To list all system/user environment variables (equivalent to
set, with cleaner formatting):Get-ChildItem Env: - To list all PowerShell automatic variables (including dynamic values like current path, date, error level, etc.):
Get-Variable
If you want a unified list, you can combine these outputs with a simple pipeline—just note that some variable names might overlap (e.g., PowerShell’s $PWD is the equivalent of cmd’s %CD%).
Why set Doesn’t Show %CD%
Just to clarify: %CD% isn’t stored in the process’s environment block—it’s a value cmd.exe calculates on the fly whenever you reference it. Since set only reads from the environment block, it can’t display these dynamic pseudo-variables.
内容的提问来源于stack exchange,提问作者LoveBug

