简体   繁体   English

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

[英]Wheres the command history in PowerShell ISE stored at?

It seems like the command history of PowerShell is stored separately from PowerShell ISE. PowerShell 的命令历史似乎与 PowerShell ISE 分开存储。 But I have one question is that where is the commands history of PowerShell ISE stored at and how do I view them?但我有一个问题是,PowerShell ISE 的命令历史存储在哪里以及如何查看它们?

As for the location of history files;至于历史文件的位置; they are located here:它们位于此处:

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
#>

As noted, in the ISE, you can just leverage the Start-Transcript cmdlet to capture all you do, but again, it is not an out-of-box/console/ISE immediate lookup of previous commands to execute like PSReadLine.如前所述,在 ISE 中,您可以仅利用Start-Transcript cmdlet 来捕获您所做的一切,但同样,它不是像 PSReadLine 那样对先前执行的命令进行开箱即用/控制台/ISE 立即查找。 Yet, you can do that, sort of, with some creativity.然而,你可以通过一些创造力来做到这一点。 Yet, that's just a kludge.然而,这只是一个杂牌。

A note about using Start-Transcript .关于使用Start-Transcript的说明。 It will default to your "$env:USERPROFILE\Documents" folder.它将默认为您的“$env:USERPROFILE\Documents”文件夹。

在此处输入图像描述

So, I'd recommend you set to go to a specific folder.因此,我建议您将 go 设置为特定文件夹。 Also, though the files can be small, there will be tons of them over time, thus, you need to manage that.此外,尽管文件可能很小,但随着时间的推移会有大量文件,因此,您需要对其进行管理。

They of course can just be opened in the ISE directly:它们当然可以直接在 ISE 中打开:

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

Yet, since you are already in the ISE, you can just type and run all commands from the editor pane, and as needed, select anyone and just run it again.然而,由于您已经在 ISE 中,您可以从编辑器窗格中键入并运行所有命令,并根据需要,select 任何人,然后再次运行它。

Yet if you are just using the ISE console, and thinking it is the same as the PowerShell consolehost, then that's wrong.然而,如果您只是在使用 ISE 控制台,并认为它与 PowerShell 控制台主机相同,那就错了。 It's really an output window with some console skills.它真的是一个 output window 与一些控制台技能。 Back in the early ISE days, there were 3 panes.早在 ISE 早期,就有 3 个窗格。 Editor, output window and true console.编辑器,output window 和真正的控制台。

旧版 PowerShell ISE 3 窗格编辑器

The true console got removed in later ISE versions.真正的控制台在后来的 ISE 版本中被删除。 Why, who knows?为什么,谁知道? VSCode almost got us back there. VSCode 几乎让我们回到了那里。

If you want to do the console stuff, then use the PowerShell console, or shell to the PowerShell console using runspaces to stay in the ISE.如果你想做控制台的事情,那么使用 PowerShell 控制台,或 shell 到 PowerShell 控制台,使用运行空间留在 ISE 中。

For the runspaces thing, here is an example to run PowerShell Core (v6+), while still in the ISE.对于运行空间,这里是一个在 ISE 中运行 PowerShell Core (v6+) 的示例。

Using PowerShell 7 in the Windows PowerShell ISE在 Windows PowerShell ISE 中使用 PowerShell 7

https://old.ironmansoftware.com/using-powershell-core-6-and-7-in-the-windows-powershell-ise 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

So, with a bit of tweaking, you can do the same for Windows PowerShell 5 to get the raw console因此,通过一些调整,您可以对 Windows PowerShell 5 执行相同操作以获得原始控制台

But again,m avoid all the hoops, and use VSCode, if you are allowed.但是,如果允许,请再次避免所有的麻烦,并使用 VSCode。 Yet on your servers, we all know ISE is still a thing.然而,在您的服务器上,我们都知道 ISE 仍然存在。 ;-} ;-}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM