Is there a windows shell tool can keep history? [c

2019-04-20 19:39发布

I use windows 7 64bit. I found both cmd.exe and powershell cannot keep history. It means it lost my command history when I quit the shell.

Is there a tools can help cmd.exe or powershell to remember the history? I try to use console 2. Console 2 is tiny and has a tab interface. But console 2 can't remember the history too. Maybe there is another front end can do this.

2条回答
啃猪蹄的小仙女
2楼-- · 2019-04-20 19:54

There's an excellent post on the Windows PowerShell Blog that provides a way of preserving command history across sessions:

http://blogs.msdn.com/b/powershell/archive/2006/07/01/perserving-command-history-across-sessions.aspx

The pertinent commands for exporting and importing the command history are Get-History and Add-History. Add the following to your PowerShell profile:

Register-EngineEvent PowerShell.Exiting {
    Get-History | Export-Csv $HOME\pshist.csv
} | Out-Null

if (Test-Path $Home\pshist.csv) {
    Import-Csv $HOME\pshist.csv | Add-History
}

This will preserve the history in such a way that you can still inspect the history for start and end times, and calculate the duration of each.

A warning though: The above script will only remember the history of the most recently exited PowerShell window. If you work with multiple shell sessions at the same time, then the history of all but one of them will be lost.

查看更多
爷的心禁止访问
3楼-- · 2019-04-20 20:13

The answer below is from Keith Hill (PowerShell Microsoft MVP) in his answer to the question powershell history: how do you prevent duplicate commands:

BTW if you want to automatically save this on exit you can do this on 2.0 like so:

Register-EngineEvent PowerShell.Exiting {
    Get-History -Count 32767 | Group CommandLine | 
    Foreach {$_.Group[0]} | Export-CliXml "$home\pshist.xml" } -SupportEvent

Then to restore upon load all you need is:

Import-CliXml "$home\pshist.xml" | Add-History
查看更多
登录 后发表回答