Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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 Archive.org, Date: May 14, 2010 Author: Jesse Webb

http://web.archive.org/web/20140330024520/http://devblog.point2.com/2010/05/14/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. :)

var-foo commented Jun 9, 2015

👍

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.

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

pl refer here - http://superuser.com/questions/302194/automatically-executing-commands-when-a-command-prompt-is-opened/302553#302553 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!

thank you you made my day 👍

@ghost

ghost commented Jan 4, 2016

This is awesome. Thanks.

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.

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

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.

erm3nda commented Jan 25, 2017 edited

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: http://superuser.com/a/238858/268652.
Also note that i've used the HKEY_CURRENT_USER not HKEY_LOCAL_MACHINE.

Thanks

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.

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