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.

15 Comments

  1. visit the following internet site

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

  2. Rasit says:

    Thanks, that's great.

  3. 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.

  4. 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 *%

  5. 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

  6. 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...

  7. 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?

  8. 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

Powered by sweet Captcha