Command Prompt in Windows does not save command history permanently by default, so once you close the window, the history is lost unless you explicitly enable it or use a workaround. To retrieve or preserve history after closing, you must either have previously enabled history logging or use backup methods like PowerShell or third-party tools—this guide covers the most reliable, built-in approach.
What You'll Need
- A Windows PC (Windows 7 or later; steps verified on Windows 10 and 11)
- Administrative rights to modify registry settings (if enabling history persistence)
- Basic familiarity with Command Prompt and Registry Editor
Step-by-Step
- Check if history was previously saved to a file—if you ran
doskey /listsize=1000in a previous session and redirected output, or used external logging, locate any text files you may have created (e.g.,history.txt). Open Command Prompt, typetype C:\path\to\history.txt(replace with your file path), and press Enter. This only works if you manually saved output earlier. - Enable persistent command history (for future sessions) by modifying the registry:
- Press
Win + R, typeregedit, and press Enter to open Registry Editor. - Navigate to:
HKEY_CURRENT_USER\Software\Microsoft\Command Processor - In the right pane, right-click a blank area > New > DWORD (32-bit) Value.
- Name it
SaveHistory. - Double-click
SaveHistory, set Base to Decimal, enter1as the value data, then click OK. - Still in the same folder, right-click again > New > String Value.
- Name it
HistoryBufferSize. - Double-click
HistoryBufferSize, set Base to Decimal, enter500(or any number up to 999), then click OK. - Close Registry Editor.
- Press
- Restart Command Prompt and run at least one new command (e.g.,
dir). Close and reopen Command Prompt to confirm the setting applies. - View the saved history file:
- Open Command Prompt.
- Type
doskey /history > %TEMP%\cmd_history.txtand press Enter. - Open File Explorer and navigate to
%TEMP%(paste this into the address bar and press Enter). - Locate
cmd_history.txtand open it in Notepad to review your recent commands.
- Use PowerShell for more robust history access (recommended for modern Windows):
- Open PowerShell as Administrator.
- Run
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced' -Name 'Start_SearchFiles' -Value 1(optional, for compatibility). - Run
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\PowerShell\Console\1' -Name 'HistorySavePath' -Value "$env:USERPROFILE\Documents\PowerShell_History.xml"to set a custom history file path. - Run
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\PowerShell\Console\1' -Name 'HistoryCount' -Value 500to increase stored history. - Close PowerShell, then reopen it and run commands.
- To view history:
Get-Content $env:USERPROFILE\Documents\PowerShell_History.xmlor useGet-Historyin a new session.
Common Mistakes
- Assuming history persists automatically—without enabling
SaveHistoryor using PowerShell, Command Prompt discards history on close by design. - Typos in registry key names (e.g.,
SvaeHistoryinstead ofSaveHistory), which cause the setting to fail silently. - Editing the wrong registry hive—ensure you are in
HKEY_CURRENT_USER, notHKEY_LOCAL_MACHINE. - Expecting old commands (from before enabling history) to appear—only new sessions after enabling will be saved.
- Confusing Command Prompt history with PowerShell history—they are separate systems with different storage mechanisms.
Tips
- Use PowerShell instead of Command Prompt for better built-in history management—it saves history to XML by default and supports
Get-HistoryandExport-Clixmlfor backups. - Set up a shortcut to open Command Prompt with a custom history size: right-click the shortcut > Properties > Options > set History Buffer Size to 500 or higher.
- Automate history export: create a batch file
save_history.batcontainingdoskey /history > %USERPROFILE%\Documents\cmd_history_$(date /t).txtand run it at the end of each session. - For advanced users, consider enabling
F7key support by adding aKeyHistoryDWORD (value 1) underCommand Processorto show history via the F7 key in future sessions. - Always back up the registry before editing (File > Export in Registry Editor).
Troubleshooting
- No history appears after enabling SaveHistory—ensure you closed and reopened Command Prompt at least once. The registry change only applies to new sessions, not the current one.
- Registry settings reset after reboot—check if Group Policy is enforced (run
gpedit.msc> Computer Configuration > Administrative Templates > System > Don’t run specified Windows applications; verify Command Prompt isn’t restricted). Also, verify no third-party security software is modifying registry keys. - History file is empty or missing—confirm you ran at least one command after enabling history. If using
doskey /history, ensure the>redirection is not blocked (e.g., by antivirus temporarily locking the TEMP folder). - Command Prompt crashes or behaves oddly after registry edits—restart your PC. If issues persist, delete the
SaveHistoryandHistoryBufferSizeregistry values and restart. - PowerShell history not saving—run
Set-ExecutionPolicy RemoteSigned -Scope CurrentUserfirst, then retry theSet-ItemPropertycommands. Also, ensure the target folder (e.g.,Documents) exists and is writable. - History appears garbled or corrupted—this may occur if multiple Command Prompt windows were open simultaneously. Use PowerShell instead, as it handles concurrent sessions better and stores history in structured XML format.