Jun 23, 2011

Command Line Aliases for Windows

Over the years, I have used Mac OS X and Unix quite a bit for work. However, my current job requires me to work with Windows XP and Windows 7. The Unix Bash shell has an alias feature which allows you to create short commands for commonly used commands. For example, typically I will setup an `ll` alias for the `ls -l` command on Unix. However, Windows doesn't have an equivalent. Or does it?

I have found that you can set up Windows batch files to work just like aliases in Unix. For example, let's say I want to set up aliases for `ls` and `ll` (which my brain is always trying use anyway). With a text editor create files that look like this:

ls.bat
`@dir /w %1 %2 %3 %4 %5 %6`

ll.bat
`@dir %1 %2 %3 %4 %5 %6`

The "@" symbol prevents the command from being echoed to the console making the batch file act just like a normal command. The "%" numbers are command line parameters passed to your command. This allows you to type `ls C:\` and still have the command work.

To use the files, they must be placed somewhere in the path. Either a directory you have added to the path or the `C:\windows\system32` directory. Once you have done this you should be able use the commands to your hearts content. Here are some examples I typically setup for all my Windows machines.

c.bat - CD to the root of the C Drive
`@C:`
`@cd c:\`

home.bat - CD to the current users home directory
`@cd %homepath%`

Simple command files like these can save you a lot of time when you work with the command prompt a lot.