Skip to content

Instantly share code, notes, and snippets.

Last active July 9, 2024 10:53
Show Gist options
  • Save vladikoff/38307908088d58af206b to your computer and use it in GitHub Desktop.
Save vladikoff/38307908088d58af206b to your computer and use it in GitHub Desktop.
Setup Persistent Aliases & Macros in Windows Command Prompt (cmd.exe) using DOSKey

Setup Persistent Aliases & Macros in Windows Command Prompt (cmd.exe) using DOSKey

Saved from, Date: May 14, 2010 Author: Jesse Webb

Our development machines here at Point2 are not standardized; we have a mixture of Windows XP, 7, and Mac OSX/Unix computers. I find myself constantly switching back and forth between command prompt interfaces when pair programming. As a result, I catch myself using “ls” to list a directories contents regardless of what system I am on. I am currently using a Windows XP machine for my developer box and I wanted to setup an alias to the “ls” command to actually perform a “dir”. Here is how I accomplished it…

There is a command available in a Window’s shell that let’s you “alias” command to whatever you please: DOSKey. It allows you to create “macros” to execute one or more other commands with a custom name. If you open up a command prompt and type in the following, you will now be able to use “ls” to list the current directory’s contents.

DOSKEY ls=dir

It also handles command line arguments, either by index or as a collection using $1 – $2 or $* respectively. This allows you to do things perform a “dir” command every time you change directories. This example would be done with the following macro definition.

DOSKEY cd=cd $1$Tdir

This all sounds very simple and easy until you close your command prompt, open a new one, and realize all of these macros you defined earlier did not persist between instances. The DOSKey command does not save these alias automatically. The DOSKey command does support saving the “currently defined macros” to a file which will allow you to run a simple command in any new shell to load macros from any saved file. The problem is, I already forget to use “dir” instead of “ls” so I know for sure I will not remember to run a certain DOSKey command every time I open up a new command prompt. I needed something more automatic.

I investigated what options I have for running commands as part of every run of cmd.exe. I found out there is a command line argument, \K, that I can use on cmd.exe to tell it to run a ‘.cmd’ or ‘.bat’ file to run commands on startup. So you can run something like to following command to load a shell instance with the following command file being ran automatically.

cmd.exe /K C:\path\to\file.cmd

This allows you to add all of the commands you want into that file and have them run automatically for the command prompt you are about to open. In order to pass this argument, I created a shortcut to cmd.exe in my Quick Launch toolbar which I could modify and use exclusively for my command prompt instances. This can easily be done by going into your C:\WINDOWS\system32 directory, right-clicking on cmd.exe and selecting “Send to Desktop”. Right click on the newly created shortcut (on your desktop) and select “Properties”. On the Shortcut tab, you will find the “Target” field which you will have to modify to include the command line option. Here is what my configuration looks like:

The only other thing from my configuration worth mentioning is the “Start in” setting I have specified. The value “%HOMEDRIVE%%HOMEPATH%” will open the command prompt in your user’s home directory as opposed to the default which opens the new window in the system32 directory which usually isn’t very helpful.

My doskey.cmd file is also worth taking a look at. It only currently has a few alias for common Unix commands but it will give you a good idea of what kind of things are capable.

@echo off

DOSKEY ls=dir
DOSKEY cd=cd $1$Tdir
DOSKEY clear=cls

It is probably best to also include the “@echo off” at the top of your script too just so you don’t have to see the noise in the shell running your script. There are also a lot more powerful features to DOSKey that I have yet to experiment with but you can see how easy it is now to add permanent macros into your command prompt.

Another thing worth noting is that you should look at the other tabs in the cmd.exe Shortcut Properties window because it makes it easy to do things like increase the buffer size of text for the shell, change font sizes and color, as well configure the behavior of command history tracking. After tweaking with all these settings, here is my new custom, improved Command Prompt:

The only “gotcha” here is that you have to open command prompt window using the Shortcut but there are other tools available which will let you run Shortcuts a simple keystroke so this should not be an issue. I hope this makes every Window’s Command Prompt user’s life a little easier. :)

Copy link

var-foo commented Jun 9, 2015


Copy link

To save some hassle with the whole shortcuts thing (in terms of a producing the equivalent of a persistent .*rc for cmd) I believe you can also use the registry to persist a .cmd file so that it starts whenever cmd.exe is run:

reg add HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft /v "Command Processor" /d "\"c:\windows\system32\cmdrc.cmd\""

The .cmd file seems to only work correctly when located within the %SYSTEMROOT%\System32 directory for your OS setup.

Copy link

reg add command above is incorrect. right key is "HKLM\software\Microsoft\command processor" and value - Autorun.

pl refer here - in answers. Also a script on different location other than system32 folder worked for me.

Also note - in case you give a invalid script file to this reg value - you may see an error message in every command prompt!

Copy link

thank you you made my day 👍

Copy link

ghost commented Jan 4, 2016

This is awesome. Thanks.

Copy link

Or you can just use PowerShell like a normal person... which has ls built-in. Why do people still use cmd.exe is beyond me.

Copy link

We could also add a keyboard shortcut like Ctrl+Alt+T in properties, completing the Linux-ishness!

Copy link

WARNING! the script referenced in the value Autorun in "HKLM\software\Microsoft\command processor" will also be executed before the command in a FOR /F statement. So if you use any scripts with FOR /F that depends on the output of the command you should make sure that the Autorun script does not produce any output.

Copy link

erm3nda commented Jan 25, 2017

Case of me (W10 x64) the command reg add HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft /v "Command Processor" /d "\"c:\windows\system32\cmdrc.cmd\"" didn't worked, without difference about the *.cmd file. I've added the key named Autorun (as someone pointed) and it worked, using *.cmd located at my home. Look at here:
Also note that i've used the HKEY_CURRENT_USER not HKEY_LOCAL_MACHINE.


Copy link

akirna commented May 11, 2017

Thanks! The Linux bash shell is so much better so I had to make some aliases for my work computer.

Copy link

I've tried to create an alias using doskey gp=C:\Program Files (x86)\gnuplot\bin\gnuplot.exe $* but it doesn't work because of the space in the file path. Please help!

Copy link

ghost commented Jun 23, 2017

@Mounica134 try this doskey gp="C:\PROGRA~2\gnuplot\bin\gnuplot.exe" $*

Copy link

bantya commented Mar 6, 2018

👍 This is awesome!

Copy link


Copy link

ssta604 commented Mar 21, 2019

My personal favorites:
doskey ls=wsl ls --color $*
doskey ll=wsl ls -l --color $*

Copy link

gotdang commented Jul 14, 2020

I didn't invent this, but I lost the original author years ago. It's a very convenient way to create an 'alias' command where you can make new macros and save and load them. I put it in a file called "%USERPROFILE%\doskey.mac".

alias=IF "$*" == "" (doskey /macros) ELSE (IF /I "$1" == "SAVE" (doskey /macros $g "%USERPROFILE%\doskey.mac" & ECHO Aliases SAVED) ELSE (IF /I "$1"=="LOAD" (doskey /macrofile="%USERPROFILE%\doskey.mac" & ECHO Aliases LOADED & doskey /macros) ELSE (doskey $*)))

Then, add this to your registry to run it automatically whenever you start a command prompt:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor]
"AutoRun"="C:\\Windows\\System32\\doskey.exe /macrofile=\"%USERPROFILE%\"\\doskey.mac"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment