Command aliases in Command Prompt?

How can I make command aliases in Windows' Command Prompt like I would with bash?

I found out about doskey in a forum thread, so I can do something like:

doskey ls=dir /b


...and now the command ls acts a little more like ls on Unix. (I type ls so often in cmd, it isn't even funny.)

But how do I get this to stick between sessions? It goes away the next time I open cmd.exe. (Is there something like .bash_profile?)

-
Sorry to make it offtopic, but you can benefit from installing a copy of cygwin and have all the unix tools at your palmrest –  bubu Jun 8 '10 at 18:06
Yeah, I'd like to, but I don't think that's an option (and might be more work than it's worth). My primary machines are Linux and OS X -- I'm just testing some programs on Windows right now. –  Benjamin Oakes Jun 8 '10 at 18:10
there isn't much thing to do. cygwin.com/setup.exe is all you need to install. just download and click next. it's all there and very simple. there isn't much to configure either. –  bubu Jun 8 '10 at 18:18
Hrm... well, I'll try it out. The machine has a lot of problems already though... ;) –  Benjamin Oakes Jun 8 '10 at 18:40
unless your MS-DOS is version 6.22 or less, you aren't running DOS. by your mention of cmd.exe i assume you're talking about the basic WinNT/2k/XP/etc command shell. –  quack quixote Jun 8 '10 at 18:56

cygwin can be used in this case although this is not exactly an on-topic answer.

http://www.cygwin.com/ CygWin

To access it easily in windows, you can put c:\cygwin\bin in your path.

note that there are a few command that clashes with windows software which is not equivalent e.g. find(1) vs find.exe -- find(1) lists all files and subdirectories whereas find.exe functions like grep.

another option is to access

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun or HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun

check out

 cmd.exe /?
for more details

-
Hrm... it doesn't seem to use any of my Windows path... I'll have to play around with this later, I think. (Never used cygwin seriously before.) Thanks for your help so far. –  Benjamin Oakes Jun 8 '10 at 19:10
adding the cygwin path into the windows path environment variable allows you to use the tools in the cygwin environment in cmd.exe; just note that there ARE caveats e.g. find.exe clashes with find command provided by cygwin. –  bubu Jun 8 '10 at 19:15
Why the undervote? I just have no idea. –  bubu May 31 '11 at 19:01

It is rather easy to setup permanent aliases in the Windows command prompt using the @DOSKEY command and HKCU\Software\Microsoft\Command Processor Autorun option.

Quick step-by-step guide:

1. Create a new batch file, call it Alias.bat. Copy/paste the text below. TIP: I recommend creating a C:\Bin folder for all your command line tools.
2. Open the register HKEY_CURRENT_USER\Software\Microsoft\Command Processor.
3. Add an String Value named Autorun and set the value to absolute path of the Alias.bat file.
4. Done.

This batch file will execute every time you open a command prompt.

Contents of Alias.bat

DOSKEY ls=DIR $* DOSKEY cp=COPY$*
DOSKEY xcp=XCOPY $* DOSKEY mv=MOVE$*
DOSKEY clear=CLS
DOSKEY h=DOSKEY /HISTORY
DOSKEY alias=if ".$*." == ".." ( DOSKEY /MACROS ) else ( DOSKEY$* )


Now you can type alias (i.e DOSKEY /MACROS) to view the current list of aliases/macros.

To add new aliases for the current session only you can use alias name=command.

-
Simple, easy to update once setup, I like this one a lot! –  Urda May 31 '11 at 14:22
Unfortunately I have since stopped using these alias, as the @DOSKEY command causes a crash when exiting Autodesk Maya 2010. There is probably a work around, e.g. checking if parent process is Maya, however I have not spent time investigating. –  Dennis Jun 3 '11 at 13:20

Also sort of off-topic -

Use PowerShell instead of the cmd.exe command line. The good news is that PowerShell has the equivalent of .bash_profile, and runs just like the cmd.exe command line. It comes with a built-in alias generation feature. The bad news is that there is a bit of a learning curve if you want to do anything more complicated than simple cmd.exe commands.

By the way, ls is defined as an alias of dir, right out of the box.

-
Why did no one ever tell me to use power-shell before? It is so much better for so many things. –  Andres May 15 '13 at 23:53

There is a registry entry at HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun which allows you to run a command when you start a cmd prompt. This includes a batch file.

-
You may want to point to HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun as others will not have the same user ID as you. –  bubu Jun 8 '10 at 19:17
@bubu: Oh, very true, sorry. I found that by searching my registry, didn't think. –  Phoshi Jun 8 '10 at 19:21

I suggest installing dos versions of these commands directly from this link.

Simple, light, efficient etc. Been using it for a while now, Makes me happy! Cygwin is a bit big for the essentials I need.

-

Try putting them in autoexec.bat. I'm not sure, you'll have to try it. Put autoexec.bat itself in C:\

-
Doesn't seem to work... –  Benjamin Oakes Jun 8 '10 at 18:19

Put your aliases into a file called, perhaps, alias.cmd and add /K \path\to\alias.cmd to the shortcut that you use to start cmd.exe.

Reference

-

I'll be a necromancer for a moment and raise this thread from the dead. It's answer was not satisfactory for me. I knew there was a better way. I dabbled at making and including bat files and trying to figure out how to get the spaghetti ball working well it didn't well. Anyway back to Google I went..

I did find this too How to add new DOS alias/commands and create a keyboard shortcut for an admin DOS It works great, it should work on 98-7 (kinda funny numbering system but hey it's windows). I hope it helps those on this thread and those Google sends this way.

This way is not as simple as Alias, and neither is doskey. Once setup this is about the same effort.

I'll also add that parts of this merged with Dennis Williamson's answer are much better. You can have shell environments for multiple versions of the same application, say php 4 for your old scripts and php 5 for your test server.

Anyhow this worked out much better for me.

-

Solving it by setting a PATH environment

create ls.c file

enter the following code

int main()
{
system("dir /b");
return 0;
}


into ls.c and compile the file:

$gcc ls.c -o ls  an executable file ls.exe is created make a directory, name it as cmdline_tools $mkdir cmdline_tools


copy ls.exe to the directory

set the path of the directory in PATH environment [copy the location of the directory and paste it in the PATH]

C:/cmdline_tools