Monday, September 15, 2014

Setting an ALIAS in Windows Command Line

I work mostly on Windows nowadays and spend a good amount of time with Windows Command Prompt. Now there are some commands , some common locations which I usually use or switch to. 

I tried looking on internet and browsing through couple of stackoverflow articles and finally this is how I solved my problem.

We can use doskey (try doskey /? to read more) for solving this problem. Common syntax to use is :

                                               doskey macroname = macrodefinition

Non Persistent Way

Use Case:

I need to frequently go to say C:\Users directory so in general I have to type cd /d C:\Users everytime. This could be a little more pain if the directory paths are very big and not so simple like C:\Users. 

Open Command Prompt and type 

                                 DOSKEY users = cd /d C:\Users





So next time whenever you need to change current directory to C:\Users just type users. It will change your current directory to C:\Users.





If you have a big list of such aliases , then you can even same all the commands in a single 
bat file say (env.bat) and just use the following command to use all the alias stored in that file.

                                              cmd /k PATH_TO_BAT_FILE

Example of a sample BAT file with aliases.





How to use :





But the big problem with this approach is that you need to do these steps everytime you open command prompt So let's see how to do this thing a one time Persistent way.


Persistent Way

Here also you need to create the bat file which will contain all the alias and doskey related information as explained above.

You need to do the following steps:

Run the ‘regedit’ command to fire up the registry editor and add the following data:
  • Key: HKCU\SOFTWARE\Microsoft\Command Processor
  • Value: AutoRun
  • Type: REG_EXPAND_SZ
  • Data: PATH_TO_BAT_FILE_WITH_ALIAS_INFO






Note:
If you want to add arguments to the commands used for alias then you can use $* at the end.

Example:
                                   python3=C:\Python31\python31.exe $*


 If you want to see list of macros defined then use doskey /macros. It will print all the doskey macros.

References: http://technet.microsoft.com/en-us/library/cc753867.aspx

2 comments:

Great. Really informative. It will save a lot of my time from now.
Thanks

Post a Comment