DOSKEY - Macros and Aliases
Typing long commands such as cd /d C:\Users\user\Documents\Projects or dir /a /o:-d over and over is tedious.
The DOSKEY command lets you give frequently used commands a short name. This mechanism is called a macro (what other shells call an alias).
Basic Usage
DOSKEY macro_name=command
Example
Create a macro so that typing ls runs dir.
doskey ls=dir $*
After registering it, typing ls shows the same result as dir.

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超 入門

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
Only the first word of the line you type is expanded as a macro. Macros also work only when you type them interactively; they cannot be used inside batch files.
Macros That Accept Arguments
In a macro definition you can use special symbols to receive the arguments typed after the macro name.
| Symbol | Description |
|---|---|
$1–$9 | The 1st to 9th argument typed after the macro name |
$* | All arguments typed after the macro name |
$T | Command separator (replaces &); chains multiple commands |
$G | Replaces the redirection symbol > |
$L | Replaces the redirection symbol < |
$B | Replaces the pipe | |
If you write >, <, | or & directly in a macro definition, they are interpreted first as redirection, pipes or command separators for the DOSKEY command itself. Use the symbols in the table instead.
Pass All Arguments ($*)
doskey ll=dir /a /o:-d $*
Typing ll C:\Windows runs dir /a /o:-d C:\Windows. For the options of dir, see
Use Individual Arguments ($1)
doskey cdd=cd /d $1
Typing cdd D:\Work moves you to D:\Work, even on a different drive.
Chain Multiple Commands ($T)
doskey mkcd=mkdir $1 $T cd $1
Typing mkcd test creates the folder test and then moves into it. Commands chained with $T run one after another even if the previous command fails.
To run the next command only if the previous one succeeds (&&), write $T$T. For more on creating folders, see
List Macros
You can view registered macros with /macros (short form: /m).
doskey /macros
Delete a Macro
To delete a macro, define it with nothing on the right side of =.
doskey ls=
Macros Disappear When You Close the Window
Macros registered with DOSKEY are valid only within that Command Prompt window. They disappear when you close it, so you would have to register them again each time.
To avoid this, save the macro definitions to a file and load it at startup.
Save Macros to a File
You can write the current macros to a file as the output of /macros. For redirection, see
doskey /macros > %USERPROFILE%\macros.doskey
The file is a plain text file with one macro per line in the form name=command. You can edit it directly in Notepad or any text editor.
ls=dir $*
ll=dir /a /o:-d $*
cdd=cd /d $1
mkcd=mkdir $1 $T cd $1
The output of /macros is written in the console code page (for example, Shift_JIS-based code page 932 on Japanese Windows). If you edit and re-save the file, use ANSI or UTF-8 without a BOM. For more on character encoding, see
Load Macros from a File
Specify the file with the /macrofile option to register them all at once.
doskey /macrofile=%USERPROFILE%\macros.doskey
Load Automatically at Startup
To load them every time you open Command Prompt, the easiest way is to change the target of a shortcut as follows.
%windir%\system32\cmd.exe /k doskey /macrofile=C:\Users\user\macros.doskey
Replace user with your own user name. The /k option keeps the window open after running the specified command.
If you set a command in the AutoRun value under HKCU\Software\Microsoft\Command Processor in the registry, it runs every time any Command Prompt starts. However, it also affects cmd instances launched from batch files and other programs, which can cause unexpected behavior. If you set it, note the previous value so you can restore it. We recommend starting with the shortcut method above.
Managing Command History
DOSKEY also manages the command history. The /history option is introduced in
| Option | Description |
|---|---|
/history | Displays the stored command history |
/listsize=n | Sets the number of commands to keep in history to n |
/macros | Lists macros |
/macros:all | Lists macros for all running executables (including non-cmd ones) |
/macrofile=file | Loads macros from a file |
/reinstall | Starts a new instance of DOSKEY and clears the history buffer |
doskey /listsize=100
Practical Example: Register Commonly Used Operations
Here is an example macro file for everyday development work.
ls=dir $*
ll=dir /a /o:-d $*
cdd=cd /d $1
mkcd=mkdir $1 $T cd $1
which=where $*
ip=ipconfig $*
which is a short way to call
ip is a short way to call .
DOSKEY macros are a feature of Command Prompt (cmd.exe). PowerShell uses different mechanisms, such as Set-Alias and functions.
Summary
| Operation | Command |
|---|---|
| Register a macro | doskey name=command |
| Pass all arguments | $* |
| Chain multiple commands | $T |
| List macros | doskey /macros |
| Delete a macro | doskey name= |
| Save to a file | doskey /macros > file |
| Load from a file | doskey /macrofile=file |