How to reuse previous commands

Overview

CMD keeps a temporary history for each open window. Recalling a command is faster and less error-prone than typing a long path again.

Press Up and Down Arrow to move through entries, then use Left, Right, Home, End, Backspace, and Delete to edit before pressing Enter.

  • F7 displays a selectable history list in the classic console host.
  • Esc clears the current input line without running it.
  • History normally disappears when that Command Prompt window closes.

Commands to try

Type each command at the prompt, then press Enter. Replace sample names and paths with ones that exist on your computer.

Commands are generally not case-sensitive, but preserving the spelling shown here makes scripts easier to read.

doskey /history
doskey /history > "%USERPROFILE%\cmd-history.txt"
notepad "%USERPROFILE%\cmd-history.txt"

Useful options and details

Options change how a command behaves. Add spaces exactly as shown, and use the command's built-in help when an option is unfamiliar.

A path containing spaces must be enclosed in double quotes, such as "C:\My Files".

  • doskey /history prints the current window's history.
  • > saves the displayed history to a text file.
  • A separate CMD window has its own history.

Tips and common mistakes

Check the current folder and read the command output before assuming an operation succeeded.

Practice with disposable files first. Commands that delete, overwrite, or stop a process may not offer an Undo button.

  • Always reread a recalled delete, move, or process command before pressing Enter.
  • Saved history can contain usernames, server names, or secrets typed on command lines; protect or delete it appropriately.
  • Do not assume history is a permanent audit log.

Safe practice

Create a temporary practice folder under your user profile so the examples cannot affect Windows system files.

After each command, inspect the result with dir or another read-only command before continuing.

  • Run three harmless commands and recall the first.
  • Edit the recalled command before running it.
  • Save history, inspect the file, and delete it when finished.