
Summary
If you use PowerShell to automate tasks on your PC and need to reuse a command, you can press the Up and Down arrow keys to cycle through the last few. But what if you want to go back to a command you typed a month ago? Microsoft thought of that too, so PowerShell saves your command history in a plain text file. It’s convenient, but it also comes with a security risk that deserves your attention. PowerShell never forgets a command Your history lives in a plain text file PowerShell keeps two separate history logs. The first is the built-in session history, which tracks the commands you run in the current window. It’s what Get-History shows you, and it’s cleared the moment the session ends and you close the window. By default, it holds up to 4,096 commands and starts dropping the oldest ones as you add more. The second is the persistent history kept by PSReadLine, the module that handles typing, editing, and command recall in the PowerShell console. If you’re on Windows PowerShell 5.1, which comes with Windows 10 and 11, or PowerShell 7, every command you type gets written to a plain text file on your drive. That’s why pressing the Up arrow in a brand new window can still bring back a command from last week. On Windows, the file lives here: %APPDATA%\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt You don’t need to remember that path. Run this in PowerShell, and it prints every command from your past sessions: Get-Content (Get-PSReadLineOption).HistorySavePath To open it in Notepad instead, press Win + R, paste the path above into the Run dialog, and hit OK. Pasting it into the File Explorer address bar works too, and %APPDATA% takes you straight past the hidden AppData folder. If you’ve used PowerShell for a while, expect a long scroll. PSReadLine only loads the most recent 4,096 entries for Up arrow recall, but nothing trims the file itself. It keeps growing from the very first command you ran in that console. PowerShell stores more than just commands Secrets you paste end up in the file too The history file can’t tell the difference between Get-Process and a password. On Windows PowerShell 5.1, whatever you type or paste into the console gets saved as is. That includes the API key you pasted to test an endpoint, a GitHub personal access token inside a git clone URL, a database connection string with the password baked in, or a quick net user bob P@ssw0rd /add to create a local account. The file isn’t encrypted, and it isn’t locked away. Any program running under your account can read it, and so can an admin. That’s exactly why attackers go looking for it. MITRE, which keeps a public catalog of attacker techniques, lists shell history as a known place to find credentials and calls out PowerShell’s persistent history by name. How exposed you are depends on your PowerShell version. Windows PowerShell 5.1 still ships with an old copy of PSReadLine that has no filter, so unless you’ve updated the module, everything goes into the file. Newer PSReadLine releases, which come with PowerShell 7, skip any line containing password, asplaintext, token, apikey, or secret. A command like ConvertTo-SecureString’ hunter2’ -AsPlainText -Force stays in session memory but never reaches the file. The catch is that the filter only looks for those words. A secret passed without them slips right through. The net usercommand above, curl -u admin:P@ssw0rd , or a key pasted as a bare argument still gets saved, even on PowerShell 7. And while Microsoft recently announced that PowerShell 7 will ship with the next Windows release, 5.1 will stay installed alongside it, so the unfiltered version isn’t going anywhere soon. Clear history doesn’t help Delete the file and change what gets saved The obvious fix is Clear-History, but it doesn’t touch the file. It only empties the session list that Get-History shows. Close the window, open a new one, press the Up arrow, and your old commands are still there. To wipe the file, delete it directly: Remove-Item (Get-PSReadLineOption).HistorySavePath PSReadLine creates a fresh file the next time you run a command. Keep in mind that each host keeps its own file, so if you use the PowerShell terminal in VS Code, there’s a separate Visual Studio Code Host_history.txt in the same folder to clean up. If you’d rather not keep a history file at all, you can turn it off: Set-PSReadLineOption -HistorySaveStyle SaveNothing That’s a heavy trade-off, though, since you lose Up arrow recall across sessions. A better middle ground is a custom AddToHistoryHandler, which decides line by line what gets written to history: Set-PSReadLineOption -AddToHistoryHandler { param([string]sensitive = ‘password|asplaintext|token|apikey|secret|net user|curl.*-u ’ return (sensitive) } Commands that match the pattern are skipped, and everything else is saved as usual, so you keep Up arrow recall for your everyday commands. Add the tools you use to the pattern. On PowerShell 7, you can return ‘MemoryOnly’ instead of PROFILE , paste the command, and save. If the profile doesn’t exist yet, create it first with New-Item -Path $PROFILE -Force . Your history is worth keeping, just not with secrets in it PowerShell’s persistent history is a handy feature, and something I’ve relied on since I discovered it had one. Because it’s plain text, you can search it like any other file. However, to reduce the security risk, you need to keep secrets out of the terminal, either with the SecretManagement module, whose Get-Secret commands the filter is built to allow, or a free password manager like Bitwarden. The file can remember everything you want, as long as there’s nothing in it worth stealing.