woodongwong / notes

4 stars 0 forks source link

PowerShell 中获取全部会话的 History #76

Open woodongwong opened 1 year ago

woodongwong commented 1 year ago

我用惯了 bash shell 中的 history 命令,发现 PowerShell 中的 history 只能列出当前会话的,通过 Get-Alias 列出所有的 alias ,发现 history 是 Get-History 的别名。

Get-History 只能列出当前会话的历史记录,想要列出所有会话的历史记录,需要查看 ConsoleHost_history.txt 文件。

ConsoleHost_history.txt 中记录了所有会话的历史记录,可以通过(Get-PSReadlineOption).HistorySavePath获取绝对路径,然后再通过Get-Content获取文件内容。

完整的命令是:Get-Content -Path (Get-PSReadlineOption).HistorySavePath,最后在profile中设置一个别名history-all,可以使用notepad $PROFILE打开 profile,将下面的代码放进去,这样就可以在 PowerShell 中使用history-all列出全部记录啦😄。

function Get-History-All
{
    Get-Content -Path (Get-PSReadlineOption).HistorySavePath
}

New-Alias history-all Get-History-All