We help IT Professionals succeed at work.

Is there a way to create alias in Windows 2008 just like in Linux Bash?

Is there a way to create aliases in DOS command prompt just like in Linux Bash?  I know of Doskey but how does it keep your information?  If I exit it gets lost.  Is there a profile that can be created to allow it to save it?
Comment
Watch Question

ChrisLead Infrastructure Architect
BRONZE EXPERT

Commented:
you can use the set command but that won't keep it out of that session

i.e.

set backupshare=\\mycompany-dc01\backup
BRONZE EXPERT
Commented:
Here is a link for you that describes how to do it.

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

Commented:
Use setx /m command line

Author

Commented:
Farzanj's looks a bit easier to work with.  

Govvy, can you give an example of how it would work with SETX? For example, how would I alias ls=dir?

Commented:
SETX LS DIR /M

Author

Commented:
This is the error I get:

C:\>setx LS DIR /M
ERROR: Access to the registry path is denied.
Steve KnightIT Consultancy
SILVER EXPERT

Commented:
Easier way probably is just add a batch file called ls to your path, i.e. enter this in notepad and call it c:\windows\ls.cmd

@dir %*

The %* will pass any command line options to the apps.

If this is Win7 etc. you would need to do it as an admin, e.g.  start cmd.exe prompt as admin then:

cd /d c:\windows\system32
echo @DIR %* > ls.cmd

ls

Commented:
Your command prompt needs to be run as Administrator

Author

Commented:
Govvy,

Didn't work as expected.


C:\>SETX LS DIR /M

SUCCESS: Specified value was saved.

C:\>LS
'LS' is not recognized as an internal or external command,
operable program or batch file.
Steve KnightIT Consultancy
SILVER EXPERT

Commented:
setx sets environment variables.... not aliases of commands, unless I am missing another use?

Author

Commented:
dragon-it,

Yeah, that's what it seems to me.  And what I'm trying to do is just use aliases that can be stored for future use.  
Steve KnightIT Consultancy
SILVER EXPERT

Commented:
Have you tried little one line (or whatever needed) batch files like suggested then?

Steve

Author

Commented:
Yeah, I tried this but it seems like I'd need a file per alias.  Is that correct?
Steve KnightIT Consultancy
SILVER EXPERT

Commented:
yes... unless you want a generic name, e.g.

u ls

so you have u.cmd whcih takes first param as shortcut etc...

but easier then to type dir!
Steve KnightIT Consultancy
SILVER EXPERT

Commented:
I did originally think I might be able to use the AppPaths area of the registry but have never been able to get it to work other than subsituting one exe for another, i.e. you can add in here:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\App Paths

or here

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\ls.exe]
 @="c:\\window\\system32\\cmd.exe"

and ls.exe will launch cmd.exe, like in the same way that if you run "write.exe" it runs wordpad for you.... BUT I haven;t been able to add any command line arguments, and you'd have to do:

cmd /c dir

Steve
Steve KnightIT Consultancy
SILVER EXPERT

Commented:
As to doskey.... you could stick a number of doskey entries in a batch file, or you can call it with a macro:

@echo off
doskey /macrofile=c:\somepath\doskey-macro.txt
cmd /k

doskey-macro.txt contains

ls=dir
etc.

You can amend registry entries so that a batch autoruns when launching a cmd.exe session or just use a shortcut to this batch to start yourself a cmd windows with them in place.

Alternatively if you haven't looked at it in the link given by fanranj above it shows similar where doskey is added to the autorun key I mention above and an alias defined of "alias".  Therefore you can add aliases in the same way as you are used to with unix shells... clever stuff!

Haven't had a chance to try it like this but looks like it could be useful.

Steve

Author

Commented:
Well, the world of registries has always been a bit of a mystery.  What all would I have to add in there?
This is following the path:
"HKEY_CURRENT_USER\Software\Microsoft\Command Processor"
Registry-Editor-2011-11-04-09-34.png
Steve KnightIT Consultancy
SILVER EXPERT

Commented:
Right click the command processor and choose New --> String Value
Value name is "AutoRun"
Value of it is "C:\somedir\yourscript.cmd"

And then in yourscript.cmd either

@doskey /macrofile=c:\somepath\doskey-macro.txt

and then list all your macros in doskey-macro.txt

or list them all one by one:

@echo off
doskey ls=dir
doskey cat=type

etc.

cat / ls / grep is about the limit of my unix shell usage so make the rest up yourself :-)
IT Consultancy
SILVER EXPERT
Commented:
Actually to use the "alias" suggestion in that link then do as above but do either do the following manually or put this in a .REG file and run it:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"AutoRun"="%userprofile%\\macros.cmd"

Open in new window


or manually:

HKEY_CURRENT_USER\Software\Microsoft\Command Processor
new --> string value called AutoRun
with value %userprofile%\macros.cmd

Open in new window


In macros.cmd in your profile dir (e.g. c:\users\stephen or c:\documents and settings\stephen ) enter

@doskey /macrofile="%userprofile%\doskey.mac"

Open in new window


An easy way to do this copy/paste from above registry entry path in the run box with notepad:
notepad "%Userprofile%\macros.cmd"

Then create the macros file:
notepad "%Userprofile%\doskey.mac"

 
alias=IF "$*" == "" (doskey /macros) ELSE (IF /I "$1" == "SAVE" (doskey /macros $g "%USERPROFILE%\doskey.mac" & ECHO Aliases SAVED) ELSE (IF /I "$1"=="LOAD" (doskey /macrofile="%USERPROFILE%\doskey.mac" & ECHO Aliases LOADED & doskey /macros) ELSE (doskey $*)))
ls=dir
cat=type

Open in new window


Launch a cmd.exe and try it...  I just did and seems to work nicely.

alias usage then is:

Alias   - shows aliases
alias SAVE -- records current aliases to doskey.mac file
alias LOAD -- load aliases back from file
alias anything else gets passed to doskey command line,

Open in new window


e.g.  

alias ls=dir
alias lsl=dir /w
alias SAVE

Steve





 

Author

Commented:
Steve,

This looks exactly like what I want!  So, I understand the "Save" part...but it doesn't look like I need to type "LOAD" to use the aliases.  When would I use that?

Author

Commented:
Very good job with explaining :-)
Steve KnightIT Consultancy
SILVER EXPERT

Commented:
sorry was away at fireworks display with the kids.... I think the idea is ... Any aliases you add is like using doskey command, i.e only for that session.  so alias save makes them permanent.  alias load i guess would only be needed if you messed up the aliases and/or wanted to put them back as they are.

Have enabled this in my own cmd prompt now too - had seen technique befoe but not as neat as with making an alias alias!

Steve
Steve KnightIT Consultancy
SILVER EXPERT

Commented:
thanks.  cant't believe all that was only 900 points mind :-(

Explore More ContentExplore courses, solutions, and other research materials related to this topic.