Is there a command to get a list of cmd commands in Windows?
When working on a computer running Windows offline (without an Internet connection), is it possible to get/generate a list of available cmd commands (including usage) through command-line?
For a list of commands:
help
And for details about a specific command:
help <command>
or
<command> /?
For example:
help xcopy
xcopy /?
You can find an official list at Microsoft Command-line reference A-Z. Besides that...
To answer your question directly, I devised a script that simply lists all .exe
files that you can execute (because they're located on your PATH
). By default, it only lists those that also reside in %WINDIR%
(unless you run it with --all
).
In a previous iteration of the script, I started every command with /?
, which is a very bad idea. Not every application on the PATH
understands that parameter. Some will simply start and remain running, instead of printing any help. So that eats up a lot of resources rather quickly.
@SETLOCAL ENABLEEXTENSIONS
@ECHO OFF
IF "%1"=="--all" (
SET LIST_ALL=TRUE
)
CALL :printPath "%PATH%"
:printPath
FOR /F "tokens=1,* delims=;" %%A IN ("%~1") DO (
IF EXIST "%%A" (
PUSHD "%%A"
FOR %%F IN (*.exe) DO (
ECHO.%%~dnpfF | FINDSTR /C:"%WINDIR%" 1> NUL
IF ERRORLEVEL 1 (
IF "%LIST_ALL%"=="TRUE" ECHO.%%~dnpfF
) ELSE (
ECHO.%%~dnpfF
)
)
POPD
) ELSE (
REM ECHO Skipping non-existent folder '%%A'
)
CALL :printPath "%%~B"
)
ENDLOCAL
So, there. This gives you a list of all available commands and their parameters. As you can already expect, it's not as useful as one might imagine.
Here's what's really important!
More interesting than the .exe
files on your PATH
are the cmd.exe
built-ins. Like IF
, FOR
and SET
. I don't have a complete list of the built-ins, but you can see most of them by running cmd.exe /?
:
DEL or ERASE
COLOR
CD or CHDIR
MD or MKDIR
PROMPT
PUSHD
POPD
SET
SETLOCAL
ENDLOCAL
IF
FOR
CALL
SHIFT
GOTO
START (also includes changes to external command invocation)
ASSOC
FTYPE
Though, at that point the help is referencing command extensions, so the list may be incomplete. Let's take a closer look at some built-ins:
FOR /?
The documentation for the FOR
command lists all the crazy parameters you can pass to FOR
. This is the go-to utility if you want to write anything related to loops.
This documentation also contains the explanation for the crazy "tilde notation":
In addition, substitution of FOR variable references has been enhanced
You can now use the following optional syntax:
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string
The modifiers can be combined to get compound results:
%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line
IF /?
IF
is the command for branching. You'll need this page because it lists the comparison operators:
If Command Extensions are enabled IF changes as follows:
IF [/I] string1 compare-op string2 command
IF CMDEXTVERSION number command
IF DEFINED variable command
where compare-op may be one of:
EQU - equal
NEQ - not equal
LSS - less than
LEQ - less than or equal
GTR - greater than
GEQ - greater than or equal
SET /?
SET
allows you to perform a wide variety of operations on variables.
The /A switch specifies that the string to the right of the equal sign
is a numerical expression that is evaluated. The expression evaluator
is pretty simple and supports the following operations, in decreasing
order of precedence:
() - grouping
! ~ - - unary operators
* / % - arithmetic operators
+ - - arithmetic operators
<< >> - logical shift
& - bitwise and
^ - bitwise exclusive or
| - bitwise or
= *= /= %= += -= - assignment
&= ^= |= <<= >>=
, - expression separator
It also allows for string manipulation through the above mentioned "tilde notation"