Command aliases in Command Prompt?
It is rather easy to setup permanent aliases in the Windows command prompt using the @DOSKEY
command and HKCU\Software\Microsoft\Command Processor
Autorun option.
Quick step-by-step guide:
- Create a new batch file, call it
Alias.bat
. Copy/paste the text below. TIP: I recommend creating aC:\Bin
folder for all your command line tools. - Open the register
HKEY_CURRENT_USER\Software\Microsoft\Command Processor
. - Add an String Value named
Autorun
and set the value to absolute path of theAlias.bat
file. - Done.
This batch file will execute every time you open a command prompt.
Contents of Alias.bat
DOSKEY ls=DIR $*
DOSKEY cp=COPY $*
DOSKEY xcp=XCOPY $*
DOSKEY mv=MOVE $*
DOSKEY clear=CLS
DOSKEY h=DOSKEY /HISTORY
DOSKEY alias=if ".$*." == ".." ( DOSKEY /MACROS ) else ( DOSKEY $* )
Now you can type alias
(i.e DOSKEY /MACROS
) to view the current list of aliases/macros.
To add new aliases for the current session only you can use alias name=command
.
Also sort of off-topic -
Use PowerShell instead of the cmd.exe
command line. The good news is that PowerShell has the equivalent of .bash_profile
, and runs just like the cmd.exe
command line. It comes with a built-in alias generation feature. The bad news is that there is a bit of a learning curve if you want to do anything more complicated than simple cmd.exe
commands.
By the way, ls
is defined as an alias of dir
, right out of the box.
There is a registry entry at
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
which allows you to run a command when you start a cmd prompt. This includes a batch file.