PowerShell ISE 中的命令历史记录存储在哪里?

buz*_*est 9 windows powershell history

PowerShell 的命令历史记录似乎与 PowerShell ISE 分开存储。但我有一个问题是,PowerShell ISE 的命令历史记录存储在哪里以及如何查看它们?

pos*_*ote 15

至于历史文件的位置;他们位于这里:

Get-ChildItem -Path "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine" | 
Format-Table -AutoSize
# Results
<#
    Directory: C:\Users\YourUserName\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine


Mode          LastWriteTime Length Name                                   
----          ------------- ------ ----                                   
-a----  17-May-21     02:23 258925 ConsoleHost_history.txt                
-a----  11-May-21     01:20 120222 Visual Studio Code Host_history.txt    
-a----  27-Jun-20     18:58      0 Windows PowerShell ISE Host_history.txt
#>
Run Code Online (Sandbox Code Playgroud)

如前所述,在 ISE 中,您可以利用Start-Transcriptcmdlet 来捕获您所做的所有操作,但同样,它不是像 PSReadLine 那样立即查找要执行的先前命令。然而,你可以通过一些创造力来做到这一点。然而,这只是一个拼凑。

关于使用Start-Transcript. 它将默认为您的“$env:USERPROFILE\Documents”文件夹。

在此输入图像描述

因此,我建议您设置为转到特定文件夹。此外,虽然文件可能很小,但随着时间的推移,它们会变得很多,因此,您需要对其进行管理。

当然可以直接在 ISE 中打开它们:

psEdit -filenames ((Get-ChildItem -Path "$env:USERPROFILE\Documents" | Sort-Object -Property LastWriteTime -Descending)[0]).FullName
Run Code Online (Sandbox Code Playgroud)

然而,由于您已经在 ISE 中,因此您只需从编辑器窗格中键入并运行所有命令,然后根据需要选择任何人并再次运行即可。

然而,如果您只是使用 ISE 控制台,并认为它与 PowerShell 控制台主机相同,那么那就错了。它实际上是一个带有一些控制台技巧的输出窗口。早在 ISE 早期,就有 3 个窗格。编辑器、输出窗口和真正的控制台。

旧版 PowerShell ISE 3 窗格编辑器

真正的控制台在后续 ISE 版本中被删除。为什么,谁知道呢?VSCode 几乎让我们回到了那里。

如果您想要执行控制台操作,请使用 PowerShell 控制台,或使用运行空间进入 PowerShell 控制台以留在 ISE 中。

对于运行空间,以下是仍在 ISE 中运行 PowerShell Core (v6+) 的示例。

在 Windows PowerShell ISE 中使用 PowerShell 7

https://old.ironmansoftware.com/using-powershell-core-6-and-7-in-the-windows-powershell-ise

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Clear()
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Switch to PowerShell 7", { 
        function New-OutOfProcRunspace {
            param($ProcessId)

            $ci = New-Object -TypeName System.Management.Automation.Runspaces.NamedPipeConnectionInfo -ArgumentList @($ProcessId)
            $tt = [System.Management.Automation.Runspaces.TypeTable]::LoadDefaultTypeFiles()

            $Runspace = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($ci, $Host, $tt)

            $Runspace.Open()
            $Runspace
        }

        $PowerShell = Start-Process PWSH -ArgumentList @("-NoExit") -PassThru -WindowStyle Hidden
        $Runspace = New-OutOfProcRunspace -ProcessId $PowerShell.Id
        $Host.PushRunspace($Runspace)
}, "ALT+F5") | Out-Null

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Switch to Windows PowerShell", { 
    $Host.PopRunspace()

    $Child = Get-CimInstance -ClassName win32_process | where {$_.ParentProcessId -eq $Pid}
    $Child | ForEach-Object { Stop-Process -Id $_.ProcessId }

}, "ALT+F6") | Out-Null
Run Code Online (Sandbox Code Playgroud)

因此,通过一些调整,您可以对 Windows PowerShell 5 执行相同的操作来获取原始控制台

但再次强调,如果允许的话,我会避免所有麻烦,并使用 VSCode。然而,在您的服务器上,我们都知道 ISE 仍然是一个重要的东西。 ;-}