Friday, July 27, 2012

How to create Command Alias (Windows)?

While working in Command Prompt and Bat scripts, I came across situations, where I feel the need to define Alias of some commands, because I’m lazy in typing long Words again and again. One such Command is “Robocopy” .. You see it’s not as long, but due to my laziness in typing it again and again, I started looking for creating Alias of Robocopy command, such as RC..
After searching on net,
Doskey is the command built-in to Microsoft Windows is used to create such Aliases and I was bit surprised, because in past I used Doskey to keep command names and then call these back without typing again and again..
Syntax: Doskey Alias=Command (executables)

And this is what I collected from a SuperUser.com’s post:
Defining Alias for dir command as ls (which is actually Unix/Linux command):

doskey ls=dir

'ls' will now do a directory listing just like dir would.
If you want to use arguments with the commands, use this syntax:
doskey db=dir /b $*

Here is another interesting example:
doskey ..=cd ..

And you can easily guess what it will do with CD .. Smile. In addition, you can Alias your own bat/cmd scripts along with parameters:
doskey whereis=c:\winscripts\whereis.cmd $*

More interestingly, you can define your aliases in a file in batch:
   1: ls=dir /ONE $*
   2: cd=cd /d $*
   3: python=python -ic ""
   4: ps=tasklist $*
   5: kill=taskkill /IM $*

And then in your bat/cmd file, let say cmd_autoruns.cmd or at command prompt, simply call it:
doskey /macrofile=c:\bin\aliases

To set your command prompt always loading your bat/cmd file defining your Aliases you can update your registry key either using “Reg” command:
reg add "hkcu\software\microsoft\command processor" /v Autorun /t reg_sz /d c:\bin\cmd_autoruns.cmd

Alternatively, using RegEdit, you can edit your registry like:
   1: REGEDIT4 
   2:  
   3: [HKEY_CURRENT_USER\Software\Microsoft\Command Processor] 
   4: "CompletionChar"=dword:00000009 
   5: “DefaultColor"=dword:00000000 
   6: "EnableExtensions"=dword:00000001 
   7: "PathCompletionChar"=dword:00000009 
   8: "Autorun"="c:\\bin\\cmd_autoruns.cmd" 
And if you wish this registry change for every user (machine level), then use this registry location:
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun

Happy Aliasing Smile with tongue out

No comments: