How to Create a Global Alias for a Command with DOSKEY
If you frequently use the command prompt, you've likely typed the same long, complex command over and over again. Creating a short, memorable alias for such a command can save a huge amount of time and effort. In the Windows command-line environment, the standard, built-in tool for creating these macros or aliases is doskey.exe.
This guide will teach you how to use the doskey command to create temporary aliases for the current cmd.exe session. More importantly, it will show you the standard method for making these aliases "permanent" and "global" by automatically loading them every time you open a new command prompt.
The Core Command: doskey
The doskey.exe utility is a command-line tool that recalls command history, edits command lines, and creates macros. A "macro" in this context is what other shells would call an "alias", i.e. a short name that expands to a longer, more complex command.
Syntax for Creating an Alias: doskey AliasName=Command
AliasName: The short, custom name you want to type.Command: The full command string you want the alias to execute.
Basic Example: Creating a Temporary Alias
Let's create a simple alias named clslog that clears the screen and then prints a timestamped log message.
First, open a command prompt (cmd.exe). Now, type the following command:
doskey clslog=cls & echo --- Log Point [%date% %time%] ---
Now, in that same command prompt window, simply type clslog and press Enter.
C:\> clslog
The screen will clear, and you will see the following output (with the current date/time):
--- Log Point [Fri 10/27/2023 15:30:12.45] ---
The Limitation: This alias is temporary. If you close this command prompt and open a new one, the clslog alias will be gone.
The Challenge: Making Aliases Permanent
doskey itself has no "save" or "permanent" switch. To make your aliases available in every new command prompt session, you must create a system where they are automatically loaded every time cmd.exe starts. This is a classic "startup script" problem.
The Solution: Using the Registry to Auto-Run a Macro File
The standard and most effective way to make your aliases permanent is a two-step process:
Step 1: Create a Macro File
First, create a simple text file (e.g., C:\MyMacros\macros.doskey) that contains all of your doskey definitions, one per line.
@echo off
REM My personal command-line aliases.
doskey c=cls
doskey h=doskey /history
doskey np=notepad++.exe
doskey ..=cd ..
doskey ...=cd ..\..
doskey e.=explorer .
doskey ls=dir /B
doskey grep=findstr /I
Step 2: Edit the Registry to Auto-Run this File
You need to add a specific value to the Windows Registry that tells cmd.exe to run a command every time it starts. This requires administrator rights.
Registry Key: HKEY_CURRENT_USER\Software\Microsoft\Command Processor
Value Name: AutoRun
Value Type: REG_SZ (String Value)
Value Data: doskey /macrofile="C:\MyMacros\macros.doskey"
You can set this with a REG ADD command from an administrative command prompt.
REG ADD "HKCU\Software\Microsoft\Command Processor" /v AutoRun /t REG_SZ /d "doskey /macrofile=\"C:\MyMacros\macros.doskey\"" /f
Now, every time you open a new cmd.exe window, this registry key will automatically execute the doskey command, loading all the aliases from your file and making them available for you to use.
Key doskey Parameters and Special Codes
When creating aliases, you can use special codes to represent arguments.
| Code | Description |
|---|---|
$* | Represents all arguments passed to the alias. |
$1 - $9 | Represents the first through ninth argument. |
$T or $t | The command separator. Allows you to chain multiple commands in a single alias. (Similar to &). |
Example using Arguments
This creates a findtext alias that runs findstr with specific, user-friendly options.
doskey findtext=findstr /S /I /N /P "$1" *.*
Usage: findtext "my search string"
Here, $1 is replaced by "my search string".
Common Pitfalls and How to Solve Them
- Admin Rights for Registry: The registry change is a one-time setup, but it requires you to be an administrator to run the
REG ADDcommand successfully. - Paths with Spaces in the Macro File: When setting the
AutoRunvalue in the registry, it is critical that the path to your macro file is properly quoted, especially if it contains spaces. TheREG ADDexample above demonstrates the correct quoting. - Aliases in Batch Scripts:
doskeyaliases do not work inside batch scripts by default. A batch file is parsed differently from an interactive session.doskeyis a tool for the interactive command line.
Practical Example: A Set of Useful, Everyday Aliases
This is a sample macros.doskey file that demonstrates a variety of useful shortcuts for a developer or system administrator.
@echo off
REM =============================================
REM = My Custom DOSKEY Command Aliases =
REM =============================================
REM --- Navigation ---
doskey ..=cd ..
doskey ...=cd ..\..
doskey ....=cd ..\..\..
doskey desk=cd %USERPROFILE%\Desktop
doskey docs=cd %USERPROFILE%\Documents
REM --- Directory Listing ---
doskey ls=dir /B
doskey l=dir /B
doskey la=dir /A /B
REM --- File Operations ---
doskey e.=explorer .
doskey md.=mkdir "%1" & cd "%1"
REM --- Network ---
doskey myip=ipconfig | findstr IPv4
doskey flush=ipconfig /flushdns
REM --- General Tools ---
doskey h=doskey /history
doskey c=cls
doskey edit=notepad.exe
Conclusion
The doskey command is an essential tool for any power user of the Windows command prompt, allowing you to create time-saving aliases for your most frequently used commands.
Key takeaways for creating permanent aliases:
- Define all your aliases in a central macro file (e.g.,
macros.doskey). - Use the
REG ADDcommand (as an Administrator) to add anAutoRunvalue to theCommand Processorregistry key, pointing to your macro file. - Use special codes like
$*and$1to pass command-line arguments to your aliases.
Once set up, this system provides a powerful and personalized command-line experience every time you open a new prompt.