Recall and edit commands at the DOS prompt, and create macros.
Syntax DOSKEY [options] [macroname=[text]] Key macroname A short name for the macro. text The commands you want to recall. Options: /MACROFILE=filename Specify a file of macros to install /MACROS Display all Doskey macros /EXENAME=exename Specify an executable other than cmd.exe /MACROS:exename Display all Doskey macros for the given executable /MACROS:ALL Display all Doskey macros for all executables ALT+F10 Clear macro definitions /HISTORY Display all commands stored in memory. /LISTSIZE=size Set the number of commands to be remembered by the buffer. (Maximum is 2,147,483,647) /REINSTALL Install a new copy of Doskey (clears the buffer).
In normal use the command line is always in overwrite mode, DOSKEY can be used to
change this to Insert, the [insert] key will always toggle from one to the other:
/INSERT By default new text you type at the command line will be inserted in old text /OVERSTRIKE By default new text you type at the command line will overwrite old /m /m: and /h are abbreviations for /macros /macros: and /history respectively. These provide compatibility with old MS-DOS versions of DOSKEY.
You cannot run a Doskey macro from a batch file, when scripting use the full path to the executable or other task you need to run.
In addition to the above, DOSKEY is loaded into memory for every cmd session so you can use Keyboard shortcuts at the command line.
A DOSKEY macro will be effective for all text input during that session, for example when entering text at a SET /P prompt, if the text starts with a macro keyword, then DOSKEY will perform a substitution, just as if you are typing at the command line. [stackoverflow]
The size of the command history can be set from Control Panel, Console or from the properties of any cmd shortcut.
Clear all history with DOSKEY /REINSTALL
In all recent versions of Windows 10 the /REINSTALL option will cause the CMD.exe session to crash.
An alternative is to manually change the ListSize:
:: Clear the command history
Doskey macro files do not support comments, but you can work around this by creating a fake macro called ; and repeatedly assigining your comment text, if you do this, always end with the line ;= to delete the fake macro.
;= here is a comment
;= another comment line
$T If you put more than one command in a DOSKEY macro, use $T. to separate them. Equivalent to & in a batch file. $1-$9 Parameters, equivalent to %1-%9 in a batch file. $* This represents ALL the parameters $1-9
By default, DOSKEY macros are only saved for the current session.
A command like:
DOSKEY /macros >C:\batch\doskey-macros.txt
Will save all the current macro definitions into a text file.
Then to restore all the macros at a later date:
To load your saved macros as soon as a new CMD session is started, you have two options:
1) Add your DOSKEY / macrofile command as an AutoRun entry in the registry
AutoRun = DOSKEY /macrofile=C:\batch\doskey-macros.txt
2) Create a shortcut to CMD.exe and add DOSKEY to it:
C:\Windows\System32\cmd.exe /k "&& DOSKEY /macrofile=C:\batch\doskey-macros.txt"
A macro to open notepad:
C:\> DOSKEY note=notepad.exe
Open a file with notepad:
C:\> DOSKEY opentext="C:\Windows\System32\notepad.exe" $1
Using the above macro:
C:\> opentext MyTextfile.txt
Switch to the H: home drive:
C:\> DOSKEY home=cd /d H:\
Switch to your folder of batch files:
C:\> DOSKEY batch=cd /d C:\Batch
A macro called 'd' to run dir/w
C:\> DOSKEY d=dir/w
A macro to disable the FORMAT command (n.b. Doskey is not a foolproof method for disabling commands, it is easily bypassed by running the full command path/ extension or creating a different macro.)
C:\> DOSKEY Format=Echo The Format command has been disabled by the administrator.
“No man steps in the same river twice, for it's not the same river, and he's not the same man” ~ Heraclitus
CMD Syntax - Batch file macros
VBScript - SendKeys
Equivalent bash commands (Linux): m4 - Macro processor, history - Command history.