直接删除 PowerShell 的历史记录文件。
根据不同版本的 PowerShell,历史文件路径如下:
- Windows PowerShell 5.1
%USERPROFILE%\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt
- PowerShell 7
$env:USERPROFILE\.local\share\powershell\PSReadLine\ConsoleHost_history.txt
删除命令示例
Windows PowerShell 5.1:
Remove-Item "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt" -Force
PowerShell 7:
Remove-Item "$env:USERPROFILE\.local\share\powershell\PSReadLine\ConsoleHost_history.txt" -Force
⚠️ 如果文件不存在,PowerShell 会报错,可以加上安全参数:
Remove-Item "路径" -Force -ErrorAction SilentlyContinue
这条命令就能彻底删除 PowerShell 历史文件,清空之前的命令记录。
如果你想,我可以帮你写一条自动检测版本并删除对应历史文件的命令,一条就搞定。