53

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.

5 Answers 5

52

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.

9
  • @nickzoum sorry I can't be more specific; please see updated answer.
    – JosefZ
    Commented Oct 13, 2016 at 15:51
  • 1
    last line, why can't we, and what can we do so that it works ?
    – v.oddou
    Commented Oct 22, 2018 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
    Commented Oct 22, 2018 at 9:44
  • 3
    WARNING: The path can not be relative to the file, it has to be absolute and the file has to continously exist there
    – Hakaishin
    Commented Sep 30, 2019 at 13:36
  • 1
    WARNING: do not forget to reopen CMD to take changes effect. Commented Jan 16, 2023 at 18:05
40
  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.

5
  • 6
    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
    Commented May 31, 2019 at 17:11
  • 1
    For me using $T messed up things. When reopened, cmd was throwing: "invalid macro definition.". So I've simply removed the $T's from the macros.doskey file and it worked. I would also like to suggest other useful shortcuts: ip=ipconfig | desktop=cd C:\Users\<your-user>\Desktop\ | gst=git status | gc=git commit | gco=git checkout | gl=gitpull | gpom=git pull origin master | gp=git push | gd=git diff | gb=git branch Commented Apr 29, 2021 at 19:27
  • it started working only if i added the registry file with the name "Autorun" Commented Sep 29, 2021 at 12:56
  • $T at the end of each line in macros.doskey, as in the example above, causes a strange prompt echo after you use an alias... e.g. if I then run "Ls" the prompt will change to "C:\Users\Joe>C:\Users\Joe>_". No need to use the "$T" at the end of each line in the macros.doskey.
    – hexaae
    Commented Oct 21 at 20:11
  • @Consta Goran "desktop=cd C:\Users\<your-user>\Desktop\" can be further refined as "desktop=cd %USERPROFILE%\Desktop\" so it won't be user-specific.
    – hexaae
    Commented Oct 21 at 20:15
6

I do it this way without registry updates. Pin "Command Prompt" to the task bar, then right click the icon on the task bar; from the popup select "Properties". In the "Target:" field enter the following:

%windir%\system32\cmd.exe /F:on /k doskey /macrofile=C:\cmds\macros.txt && Title CONSOLE

Then create the C:\cmds\ folder, open a command prompt and set up your desired "doskey" macros:

doskey ip=ipconfig
doskey ls=dir /w

Last, create the macro file:

doskey /macros > C:\cmds\macros.txt

This will change the "Command Prompt" icon in the task bar to invoke doskey using the macrofile on startup. You can use the "Command Prompt" in the START menu if you don't need the macros.

1
  • This is not specific to the Task Bar, though. You could have multiple shortcuts to cmd.exe in the Start Menu - or anywhere else - and change the properties to your liking on each and every one of them.
    – Lumi
    Commented Jul 23, 2023 at 18:33
5

For anyone who wants to use doskey macros in conjunction with clink, you can simply follow this and add

&& doskey.exe /macrofile="C:\Your\Path\To\Your\DosKeyFile"

to the regkey "AutoRun" in

\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor

Mine looks like this,

"C:\Your\Path\To\Your\clink.bat" inject --autorun && doskey.exe /macrofile="C:\Your\Path\To\Your\DosKeyFile"

Yours can differ.

source

2
  • @zx485: Congratulations on reaching 2000 reputation points.  (Yes, I see that it actually happened several weeks ago.)  Actually, I believe that you did make three small errors when you edited this post; I have fixed them. Commented Feb 7, 2020 at 2:19
  • @G-ManSays'ReinstateMonica': Sorry for my mistakes. I was unsure in some parts. Thanks for correting them.
    – zx485
    Commented Feb 7, 2020 at 2:22
0

DOSKEY macros can also be added to a 'shortcut' used to start the command window. For example, I added a "np" macro to start notepad++ using this method.

  • Right-click on the CMD shortcut
  • Select "Properties
  • In the "Target field" append the following: /K doskey np="c:\Program Files (x86)\Notepad++\notepad++.exe" "$*"

It should look something like this: %windir%\system32\cmd.exe /K doskey np="c:\Program Files (x86)\Notepad++\notepad++.exe" "$*"

When you start a cmd windows from this modified shortcut, typing np will bring up notepadd++. You would need to adjust as necessary for your desired commands.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .