While PowerShell and modern terminal shells provide aliases like ls for directory listings, developers and IT administrators frequently work within native Windows Command Prompt (cmd.exe) environments when compiling legacy codebases, executing batch automation scripts, or administering bare-metal enterprise servers. In Command Prompt, typing long file paths, complex tool commands, or recurring utility flags slows down daily administration. Windows includes a battle-tested, native utility to solve this: doskey.
The doskey command allows users to recall previously executed commands, manage history buffers, and create custom command-line aliases (macros) that streamline repetitive terminal commands.
What is a doskey Macro?
A doskey macro is a user-defined alias that intercepts command-line input before Command Prompt evaluates it. When you type the macro name, doskey automatically substitutes your defined command string, including custom flags, directory paths, and piped operations. Unlike batch scripts (.bat files) which require disk reads and separate sub-processes, doskey macros are stored directly in Command Prompt memory, executing with zero initialization latency.
Basic Macro Syntax and Special Parameter Codes
The standard syntax to define a macro is structured as follows:
doskey macroname=command [options]
To pass arguments or execute multi-command pipelines inside a macro, doskey uses special dollar-sign ($) meta-characters:
| Special Code | Operational Meaning | Practical Use Case |
|---|---|---|
$* |
Captures all trailing arguments passed after the macro name. | Forwarding file paths and switches to the underlying executable. |
$1, $2, ... $9 |
Positional arguments (substitutes specific input words). | Passing structured arguments to target commands. |
$t |
Command separator (equivalent to & in batch). |
Executing multiple sequential commands within one single macro alias. |
$b |
Pipe character (equivalent to |). |
Piping macro output directly into utilities like more or findstr. |
$g |
Output redirection (equivalent to >). |
Redirecting output to overwrite a file. |
$g$g |
Append redirection (equivalent to >>). |
Appending output to log files. |
Essential Examples: Creating Practical Aliases
1. Creating Unix-Style Directory and Screen Commands
Create familiar terminal shortcuts to list directories and clear the screen:
doskey ls=dir /b $*
doskey ll=dir /o:d $*
doskey clear=cls
2. Multi-Command Sequential Macro
Create an alias that switches to a development directory, clears the terminal, and displays Git status in one keystroke using $t:
doskey dev=cd C:\Projects\MyApp $t cls $t git status
3. Macro with Piped Output
Create an alias to quickly search running processes using $b (pipe) and $*:
doskey ps=tasklist $b findstr /i $*
You can then execute ps chrome to instantly filter all running Google Chrome processes.
Viewing and Purging Active Macros
To audit or manage your active macro configuration:
- Display All Active Macros:
doskey /macros - View Command History:
doskey /history - Delete a Specific Macro: To remove an alias from memory, assign it to an empty value:
doskey ls=
Making doskey Macros Permanent Across All Terminal Sessions
By default, doskey macros exist only within the current cmd.exe session. To load them automatically whenever you open a new Command Prompt:
- Create a persistent macros definition file (e.g.,
C:\Users\Public\macros.txt) containing your aliases:ls=dir /b $* clear=cls flushdns=ipconfig /flushdns nstat=netstat -ano $b findstr /i $* - Open Registry Editor (
regedit) or run the following command in Terminal as Administrator to configure the AutoRun registry string:reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun /t REG_EXPAND_SZ /d "doskey /macrofile="C:\Users\Public\macros.txt"" /f - Open a fresh Command Prompt window. Your custom macros will now load automatically every time.
Frequently Asked Questions
Can doskey macros override built-in internal commands like cd or dir?
Yes. If a macro shares the same name as an internal Command Prompt binary (such as dir), doskey intercepts and executes the macro instead. To bypass the macro and execute the original internal command, type a leading space before the command: dir.
Do doskey macros work inside PowerShell?
No. doskey is a legacy subsystem built for cmd.exe. In PowerShell, create persistent aliases using the Set-Alias cmdlet or define custom functions in your $PROFILE script.