关于历史文件的位置,它们位于这里:
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
#>
值得注意的是,在ISE中,您只需利用Start-Transcript
cmdlet来捕获您所做的一切,但需要注意的是,它并不像PSReadLine那样立即查找以前执行的命令。然而,您可以通过一些创意实现类似的功能,但这只是一个临时的解决方法。
关于使用Start-Transcript
的说明。它将默认到您的"$env:USERPROFILE\Documents"文件夹。
因此,我建议您设置为转到特定的文件夹。此外,尽管这些文件可能很小,但随着时间的推移,文件数量会变得非常大,因此您需要管理它们。
当然,它们也可以直接在ISE中打开:
psEdit -filenames ((Get-ChildItem -Path "$env:USERPROFILE\Documents" | Sort-Object -Property LastWriteTime -Descending)[0]).FullName
然而,既然您已经在ISE中,您可以直接从编辑窗格键入和运行所有命令,并根据需要选择任何一个并再次运行。
但是,如果您只是使用ISE控制台,并认为它与PowerShell控制台宿主程序相同,那就是错误的。它实际上是具有一些控制台技能的输出窗口。在早期的ISE版本中,有3个窗格:编辑器、输出窗口和真正的控制台。
后来的ISE版本中删除了真正的控制台。为什么呢?谁知道呢?VSCode几乎让我们回到了那里。
如果您想要做控制台的事情,那么请使用PowerShell控制台,或者使用runspaces将其连接到PowerShell控制台以保持在ISE中。
关于runspaces的事情,在这里有一个示例,可以在仍在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
所以,稍加调整,您可以对Windows PowerShell 5执行相同的操作,以获取原始控制台。
但是,如果允许的话,请使用VSCode来避免所有的麻烦。然而,在您的服务器上,我们都知道ISE仍然是一个实用工具;-}
Get-History
命令来查看命令历史记录。我相信它只存储在该会话的内存中,所以一旦关闭,就无法再访问了。不过你可以通过更改 ISE 属性或使用$MaximumHistoryCount = 1000
来增加缓冲区大小。 - Abraham Zinala(Get-PSReadlineOption).HistorySavePath
命令来查找。它保存在你的 appdata 文件夹下。因此,使用Cat (Get-PSReadlineOption).HistorySavePath
命令来获取所有历史命令。 - Abraham ZinalaGet-History
起作用了,但是 PSReadline 没有。很好的发现,我试图在普通的 posh 控制台上运行它。 - Abraham Zinala