Setup Persistent Aliases & Macros in Windows Command Prompt (cmd.exe) using DOSKey
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.


Share on Facebook
Thanks, Jesse. You made my day. I really like customized alias commands in UNIX/Linux environment. It’s good to find a way to get similar thing in Windows.
If you save your doskey macros to a file, then you can have them installed automatically when the command shell starts by adding a REG_EXPAND_SZ to the registry at HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun. Set this string to <> and your macro commands will be available whether you start them via “run cmd” or a cmd icon. Or you could call your doskey.cmd file using this setting (although it seems less efficient than calling DOSKEY once).
It turns out that cmd.exe has an honest-to-god init file, just like a ~/.zshrc kinda thing… It’s just that the feature isn’t turned on by default! It just needs turning on in the Registry (which I call the Special People Filesystem).
I found out about this when I was digging around in the normally unhelpfully noisy output of “cmd /?”. So, you have to specify a filespec in the registry key
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
and/or the registry key
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
and then that filespec (presumably always to .bat) is run every time cmd starts. (Currently, the value is “”.)
I have a c:\utils directory with personal stuff in it, and so I fired up regedit and set HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor to c:\utils\_init.bat
A probably smarter name to go with would be %HOMEDRIVE%%HOMEPATH%\cmd_init.bat which might require quotes around it. Or maybe put it in %APPDATA%\cmd_startup.bat — you get the idea.
Regardless of where my init file goes, I have it set up with some handy aliases like you do, and the world’s a better place because of it.
For serious unixery, you could write an ls.pl (or whatever) in Perl and do: doskey ls=perl -S ls.pl $*
Of course, at some point you lose your mind and need cygwin, but that solves some problems while creating others.
The large print giveth and the small print taketh away!
I am glad other people are getting some mileage out of this not-so-obvious feature of the CMD program. Thank you for your suggestions to help make these aliases available in all terminals and not just the ones started by the shortcut. I don’t usually like messing around in the Registry but this seems like the perfect reason to get my hands dirty.
You can emulate a UNIX-style alias command in Windows with the following:
doskey alias=if “.$*.” == “..” ( doskey /MACROS ) else ( doskey $* )
Then again, it would be so much easier if Microsoft were to accept the decades of experience of the Unix community and adopt a compatible shell…
Genius!
Hey Jesse,
Great work…. Keep up the good work!
I realize this is old, but: if using multiple macros, why are you invoking doskey multiple times instead of using doskey /MACROFILE=%myMacroFile%
Using a macrofile is probably a more efficient usage but this article was written when I just found out about DOSKEY and I wasn’t aware of the more advanced options. Either way will let you set-up some nice aliases. Thanks for the tip!