Aliases in Windows command prompt

Learn aliases in windows command prompt with practical examples, diagrams, and best practices. Covers windows, notepad++, alias development techniques with visual explanations.

Mastering Aliases in Windows Command Prompt

Hero image for Aliases in Windows command prompt

Learn how to create and manage command aliases in Windows Command Prompt (CMD) to streamline your workflow and boost productivity.

While Linux/Unix systems have long embraced the power of command aliases, Windows Command Prompt (CMD) also offers similar functionality, albeit through a slightly different mechanism. Aliases allow you to define shorthand commands for longer, more complex commands or sequences of commands, significantly speeding up your work in the terminal. This article will guide you through setting up and using aliases in CMD, enhancing your command-line experience.

Understanding Aliases in CMD

Unlike PowerShell, which has a native Set-Alias cmdlet, CMD relies on a feature called 'Doskey macros' to achieve alias-like behavior. Doskey is a utility that loads into memory and allows you to create macros (aliases), recall commands, and edit command lines. These macros are essentially custom commands that execute one or more predefined commands. They are particularly useful for frequently used commands or for creating custom tools.

flowchart TD
    A[User types alias] --> B{Doskey intercepts command?}
    B -- Yes --> C[Execute predefined macro]
    B -- No --> D[Execute original command]
    C --> E[Command executed]
    D --> E

How Doskey intercepts and executes aliases in CMD

Creating Temporary Aliases with Doskey

The simplest way to create an alias in CMD is by using the doskey command directly in your command prompt. These aliases are temporary and will only last for the duration of the current CMD session. Once you close the command prompt window, the aliases will be lost.

doskey ll=dir /b
doskey gs=git status
doskey npp="C:\Program Files\Notepad++\notepad++.exe" $*

Examples of creating temporary aliases using doskey

Making Aliases Permanent

For aliases to persist across CMD sessions, you need to save them to a file and then load that file every time a new command prompt opens. This is typically done by creating a .bat file and configuring the command prompt to run it on startup.

1. Create an Alias File

Open a text editor (like Notepad++) and create a new file named aliases.bat (or any other descriptive name) in a convenient location, such as your user profile directory (%USERPROFILE%).

2. Add Alias Definitions

Inside aliases.bat, add your doskey commands, one per line. For example:

3. Configure CMD for Startup

To make CMD load your aliases automatically, you need to modify the registry. Open the Registry Editor (regedit.exe) and navigate to HKEY_CURRENT_USER\Software\Microsoft\Command Processor. Create a new String Value named AutoRun and set its data to doskey /macrofile="%USERPROFILE%\aliases.bat". Make sure to use the correct path to your aliases.bat file.

4. Verify Aliases

Close and reopen your Command Prompt. Type doskey /macros to see a list of all currently defined macros. Your permanent aliases should now appear.

REM aliases.bat
doskey ll=dir /b
doskey gs=git status
doskey npp="C:\Program Files\Notepad++\notepad++.exe" $*

Example content for aliases.bat

Managing and Listing Aliases

Doskey provides commands to manage your aliases effectively. You can list all defined macros, delete specific ones, or clear all of them.

doskey /macros         REM Lists all defined macros
doskey ll=             REM Deletes the 'll' macro
doskey /reinstall      REM Clears all macros and reloads doskey

Commands for managing Doskey macros