10

I think the title is pretty self explanatory.

The question is how can I create an alias in a Windows cmd that will also work after I close and then reopen it. For example I would like an alias called ip to call the command ipconfig or an alias ls -l to call Dir.

My operating system is Windows 10, in case that makes a difference.

11

Create a macro definition file, for instance in notepad; name it at will and save it anywhere (for instance, in next example I used filename macros.doskey in d:\bat\ folder).
Alternatively, doskey /macros>d:\bat\macros.doskey command will list all current macro definitions into d:\bat\macros.doskey file.
A sample macro definition file could be as follows (note that ==> is my command prompt specified by prompt $Q$Q$G$S command):

==> type d:\bat\macros.doskey
ls=dir /B $1 
ip=ipconfig $*

Then, next commands should do the job:

==> reg add "HKCU\Software\Microsoft\Command Processor" /v Autorun /d "doskey /macrofile=\"d:\bat\macros.doskey\"" /f
The operation completed successfully.

==> reg query "HKCU\Software\Microsoft\Command Processor" /v Autorun

HKEY_CURRENT_USER\Software\Microsoft\Command Processor
    Autorun    REG_SZ    doskey /macrofile="d:\bat\macros.doskey"

For explanation, read cmd /?:

If /D was NOT specified on the command line, then when CMD.EXE starts, it looks for the following REG_SZ/REG_EXPAND_SZ registry variables, and if either or both are present, they are executed first.

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun

and/or

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun

Disclaimer: some AutoRun settings could eventuate in unlooked-for unwanted behaviour, e.g. as decribed in Hidden gotcha: The command processor’s AutoRun setting

Read Save and restore macro definitions; you could prepare a valid macros.cmd script file in one step:

==> >macros.cmd (@for /F "delims=" %G in ('doskey /macros') do @echo DOSKEY %G)

==> type macros.cmd
DOSKEY ip=ipconfig $*
DOSKEY ls=dir /B $1

==>

Please keep in mind that you cannot run a Doskey macro from a batch file.

  • @nickzoum sorry I can't be more specific; please see updated answer. – JosefZ Oct 13 '16 at 15:51
  • last line, why can't we, and what can we do so that it works ? – v.oddou Oct 22 '18 at 8:33
  • @v.oddou please read doskey.exe and Batch file macros as well as both useful links to dostips.com below in the latter. – JosefZ Oct 22 '18 at 9:44
  • 1
    WARNING: The path can not be relative to the file, it has to be absolute and the file has to continously exist there – Hakaishin Sep 30 at 13:36
8
  1. Create a file to store your macros (DOSKEYs).
    "C:\bat\macros.doskey"

    ls=dir $* $T
    up=cd.. $T
    ex=exit $T
    np=notepad
    
  2. Go to the registry editor.

    HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\
    
  3. Right-click and add a new "String Value" sub-key. Name it Autorun.
    Right-click -> New -> String Value

  4. Right-click it and Modify the Value data.
    Right-click -> Modify -> Value data -> DOSKEY /MACROFILE="C:\bat\macros.doskey"
  5. Good to go.

† Note that the file does not have to be saved as a .doskey file.
† Also note that the token $T is required if you're making multiple DOSKEYs.

  • 2
    Apparently the $T is only needed if multiple commands are used in a single DOSKEY macro. You use $T to separate the commands, similar to using & in a batch file. – melston May 31 at 17:11
0

You can use & or && between multiple commands in many cases but enclosing the full sequence between parentheses allows you to use & and && in all cases. If you can remember part of a file name you wish to locate on your C drive, try:

  1. L=echo. & echo Drive C & echo. & dir c:\"*$**" /s /b

  2. L=(echo. & echo Drive C & echo. & dir c:\"*$**" /s /b)

New contributor
Richard Steckler is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Not sure how this is relevant to the question – nick zoum Nov 11 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.