Is there a windows shell tool can keep history? [closed]

落爺英雄遲暮 提交于 2019-12-05 01:52:37
Michael Kelley

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

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!