Adding an alias in Windows 7 or making ls = dir in a command prompt

Hey all,

I don’t know about you but I switch between FreeBSD and Windows a lot.  So it drives me crazy when I type the command ls on windows and get the error message.

C:\Windows\system32>ls
‘ls’ is not recognized as an internal or external command,
operable program or batch file.

So I want this to go away.

I looked for the alias command in Windows and couldn’t find one.  So I made a batch file that solves this.

Windows doesn’t seem to have the equivalent of a .shrc or .cshrc or .bashrc. I couldn’t find a .profile either.  So I decided to go with the batch file route.

Option 1 – Using doskey

I was tipped off to this idea from a comment, which led my mind to the Command Prompt autorun registry I already knew about. But once I wrote the batch file, the parameters were not working, so I searched around and found an example of exactly what I wanted to do here:
http://bork.hampshire.edu/~alan/code/UnixInWin2K/

  1. Create a batch file called autorun.bat and put it in your home directory:
    My home dir is: c:\users\jared
  2. Add the following to your autorun.bat.
    @ECHO OFF
    doskey ls=dir /b $*
    doskey ll=dir $*
    doskey cat=type $*
    doskey ..=cd..
    doskey grep=find "$1" $2
    doskey mv=ren $*
    doskey rm=del $*
    
  3. Add the following key to the registry:
    Key:  HKEY_CURRENT_USER\Software\Microsoft\Command Processor
    REG_SZ  (String): Autorun
    Value:  %USERPROFILE%\autorun.batOr as a .reg file:

    Windows Registry Editor Version 5.00
    
    [HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
    "Autorun"="%USERPROFILE%\\autorun.bat"
    

Now whenever you open a command prompt, the aliases will be there.

Option 2 – Creating a batch file as an alias

I created an.bat file that just forwards calls the original file and forwards all parameters passed when making the call.

Here is how it works.

Create a file called ls.bat. Add the following text.

ls.bat

@ECHO OFF
dir $*

Copy this batch file to your C:\Windows\System32 directory. Now you can type in ls on a windows box at the command prompt and it works.

How does this work to make your aliased command?

  1. Name the batch file the name of the alias.  I want to alias ls to dir, so my batch file is named ls.bat.
  2. In the batch file, set the RealCMDPath variable to the proper value, in my case it is dir.

So if you want to alias cp to copy, you do this:

  1. Copy the file and name it cp.bat.
  2. Edit the file and set this line:
    SET RealCMDPath=dir

Now you have an alias for both ls and cp.

Using different versions of msbuild.exe

You can also use this so you don’t have to add a path.

I need to use C:\Windows\Microsoft.NET\Framework\v3.5\msbuild.exe but sometimes I want to use C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe. Both files are named the same. So I can easily use my alias command.

  1. Create two files in C:\Windows\System32: one named msbuild35.bat and one named msbuild40.bat.
  2. Change the line in each file to have the appropriate paths for the RealCMDPath.

Anyway, this is really a useful batch file.

21 Comments

  1. Tamela says:

    Everytyhing is vry open witth a vesry clear description off the challenges.
    It wass trujly informative. Youur websit is extremmely helpful.
    Thank yoou forr sharing!

  2. Tim says:

    Old post, I know, however, if you only wish to execute a small list of aliases, such as ls and pwd, then you could do it this way:
    Windows 7 or Windows 8
    Locate the shortcut that you use to launch the command prompt. If you don't have a shortcut, create one, as you want to use the shortcut properties to auto-launch your doskey options.
    To find the likely existing command prompt shortcut:
    Start -> search for app "command"
    Under Apps, you should have found Command Prompt. Right-click on it.
    Choose Show in Explorer.
    (This will likely be in your Start menu profile path; it may not exist under All Users*)
    Right-click the Command Prompt shortcut.
    In the Target field, use the /k switch to specify an initial command to run; use && to list commands:
    e.g. %windir%\system32\cmd.exe /k doskey ls=dir&&doskey pwd=powershell pwd&&doskey ll=powershell ls

    (Here the use of 'powershell' is an example only; use whatever list of commands that you wish).

    If you have a longer list of doskeys you wish to use, and you do not feel like using a batch file or messing with the registry, you can use the doskey /macrofile option to specify a text file that lists your doskeys:

    %windir%\system32\cmd.exe /k doskey /macrofile=c:\users\someuser\doskeylist.txt

    The file doskeylist.txt (or whatever filename you choose, under whatever path you choose) is simply a list of doskey aliases, such as this (in line with my example above):

    ls=dir
    pwd=powershell pwd
    ll=powershell ls

    I just figured this out and tested it, so there are likely smoother ways to do this if you wish to increase functionality. For a simple alias/doskey, however, modifying the shortcut target to execute your doskey is elegant in its simplicity, as it can be done completely in the shortcut. As well, this is the same place, under the Advanced button, that you can have cmd.exe run as administrator at launch, instead of by right-clicking. Use wisely.

    • Tim says:

      *For all users, you may not be able to navigate to C:\Users\All Users\Start Menu\, as Access may be Denied, however, you should be able to navigate directly to C:\Users\All Users\Start Menu\Programs\System Tools. There may not be a Command Prompt shortcut, however, you likely can create one there and have it available for all users.

      • Tim says:

        Of course, the obvious thing about the post is that, if you have powershell, you could just launch powershell and said aliases like ls and pwd are there. In powershell, type "Alias" to see a list of aliases and "Alias -?" for a list of commands with regards to Alias, and in general -? for help with . Type "help" for general powershell help. If you haven't done so, type Update-Help to get the complete help files).

        -Captain Obvious(ly it's late and I'm not paying attention)

  3. visit the following internet site says:

    visit the following internet site

    Adding an alias in Windows 7 or making ls = dir in a command prompt | Rhyous

  4. Rasit says:

    Thanks, that's great.

  5. matt says:

    or even easier:

    doskey ls=dir

    getting it to stick is a bit trickier, this seems to be the neatest way i've found (assuming you're keen to stick with vanilla cmd.exe like i am)

    http://darkforge.blogspot.com/2010/08/permanent-windows-command-line-aliases.html

    • Rhyous says:

      Thanks Matt! That is the best solution I have seen yet.

      I updated my post.

      • James says:

        The new version posted is really nice and makes cmd.exe a lot more bearable. I've aliased a bunch of stuff this way.

        It's a shame you can't pipe to aliases, it seems. type example.txt | grep "blahblah" tells me grep doesn't exist, even when it's aliased. Of course, I could just do find "blahblah" example.txt, but it's still sad I can't pipe to aliases.

  6. Scott Stafford says:

    Your batch file can be enormously simplified with %*, which is "all parameters". So a batch file called ls.bat with this in it is sufficient:

    @dir *%

  7. Nathan Spitzer says:

    You are doing it the hard way. Add the following registry key:
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\.exe]
    @="c:\\"

    You can then run the alias directly from the "run" box or do start from a dos box. Be sure to double-slash in the path as shown

  8. CaptWheeto says:

    You could always use Powershell. It seems the guys who designed it were influenced a lot by bash, also you can edit your own profile to include more aliases. It has ls and rm and a couple of other things that make working on Windows easier. If only I could use Linux/FreeBSD at work...

  9. Chris says:

    The alias command in Windows is called "alias". Just type "alias" to see all the aliases that currently exist, or "alias /?" to see how to add an alias. You can also create a file containing a whole boatload of aliases, and then feed that file to the alias command, like this: "alias -f alias.txt".

    Aliases can contain environment variables, but aren't recursive (can't contain other aliases), AFAIK.

    -- Chris

    • rhyous says:

      Chris,

      I opened a command prompt and typed in alias.

      C:\Users\Jared>alias
      'alias' is not recognized as an internal or external command,
      operable program or batch file.

      Does it exist on Windows 7 64-bit?
      Or do you have to install something first?

  10. Martim says:

    I used to use UWIN from AT&T. But I don't installed it in Windows 7 ...

    http://en.wikipedia.org/wiki/UWIN

    http://www2.research.att.com/sw/tools/uwin/

Leave a Reply

How to post code in comments?