How to Get the Command Processor Command Line with %CMDCMDLINE%
The Windows Command Processor (cmd.exe) has several built-in, dynamic variables that provide information about its own state and how it was launched. One of these is %CMDCMDLINE%. This special variable contains the exact, original command line that was used to start the current instance of cmd.exe.
This guide will explain what %CMDCMDLINE% represents, show you how to use it, and demonstrate its most practical use case: determining if a batch script was launched by a user double-clicking it or by being run from an existing command prompt.
What is the %CMDCMDLINE% Variable?
The %CMDCMDLINE% variable is a read-only, dynamic variable that expands to the full, unmodified command line used to invoke the current cmd.exe process. It includes the full path to cmd.exe itself and any switches or commands it was launched with.
This is different from %0 or %*:
%0: The name of the batch script itself.%*: The arguments passed to the batch script.%CMDCMDLINE%: The command line that started the entirecmd.exesession that is running your script.
Basic Example: Displaying the Command Line
The simplest way to understand this variable is to see it in action.
@ECHO OFF
ECHO The command line for this CMD session is:
ECHO %CMDCMDLINE%
ECHO.
PAUSE
Now, let's run this script in two different ways.
Scenario 1: Run from an existing Command Prompt
- Open
cmd.exemanually. - Navigate to the directory where your script is saved.
- Run the script by typing its name.
Output:
The command line for this CMD session is:
"C:\WINDOWS\system32\cmd.exe"
Press any key to continue . . .
Here, %CMDCMDLINE% shows the simple command that started the interactive cmd.exe session.
Scenario 2: Run by Double-Clicking the .bat file
- Find the script file in Windows Explorer.
- Double-click the file to run it.
Output:
The command line for this CMD session is:
cmd /c ""C:\Users\YourUser\Desktop\MyScript.bat" "
Press any key to continue . . .
This output is much more interesting and is the key to this variable's usefulness.
How to Interpret the Output
When you double-click a .bat file, Windows doesn't just run the script. It starts a new, temporary cmd.exe process with a specific command line to execute your script.
Let's break down the output from Scenario 2: cmd /c ""C:\Users\YourUser\Desktop\MyScript.bat" "
cmd: The program that was started./c: A switch that tellscmd.exeto carry out the command specified by the following string and then terminate.""C:\...MyScript.bat" ": This is the command thatcmd /cis executing. It's a quoted string containing the full path to your batch file.
The presence of the /c switch and the name of your script within %CMDCMDLINE% is a clear indicator that the script was launched directly, not from an existing, interactive command prompt.
Practical Use Case: Detecting How a Script Was Launched
The most common use for %CMDCMDLINE% is to make a script behave differently based on how it was started. A classic example is a script that should not just flash on the screen and disappear if a user double-clicks it.
The logic is: if the script was double-clicked, %CMDCMDLINE% will contain the script's own name. We can check for this and add a PAUSE command only when needed.
@ECHO OFF
ECHO --- My Application ---
ECHO Processing data...
ECHO Done.
ECHO.
REM --- Check if the script was run by double-clicking ---
REM We use FINDSTR to see if the script's own name (%~nx0) is inside %CMDCMDLINE%.
ECHO %CMDCMDLINE% | FINDSTR /I /C:"%~nx0" > NUL
IF %ERRORLEVEL% EQU 0 (
ECHO This script was likely double-clicked.
ECHO Press any key to exit.
PAUSE
)
%~nx0: This variable expands to the name and extension of the current script (e.g.,MyScript.bat).FINDSTR /I /C:"...": This searches for the literal (/C) script name inside the%CMDCMDLINE%string, ignoring case (/I).- If it's found (
ERRORLEVELis 0): The script knows it was likely double-clicked, so it pauses to allow the user to see the output. - If it's not found (
ERRORLEVELis 1): The script assumes it's being run from an existing command prompt, where a pause is unnecessary, and it exits cleanly.
Common Pitfalls and How to Solve Them
- Complexity: The raw output of
%CMDCMDLINE%can be complex and includes extra quotes. Parsing it for specific details can be tricky. For most cases, simply checking for the presence of your script's name is the most reliable approach. - Not a Security Tool: This check is for user convenience, not security. A user could craft a command line that bypasses the check. Do not rely on it to control access or critical logic.
- Delayed Expansion: If you use this variable inside a loop with delayed expansion, remember that
%CMDCMDLINE%is expanded statically. Its value will not change during the script's execution.
Conclusion
The %CMDCMDLINE% variable is a special tool that provides a window into how the current command processor session was initiated.
Key takeaways:
- It contains the full command line used to start the current
cmd.exeprocess. - Its value is different when a script is run from an existing prompt versus when it is launched by a double-click.
- Its most practical use is to detect if a script was double-clicked so you can add a conditional
PAUSEat the end, providing a better user experience.
While not a variable you'll use every day, %CMDCMDLINE% is the perfect solution for this specific and very common scripting problem.