powershell - 在 Windows 10 Powershell 中通过 F7 使命令历史弹出窗口工作

标签 powershell windows-10

关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

3年前关闭。




Improve this question




在 Powershell 控制台中,在 Windows 10 (10.0.17134.0) 上,F7 不会像在 CMD 控制台中那样弹出命令历史记录。我们如何在 PS 控制台窗口中完成这项工作?关于 $Profile 的任何提示这样做的语法?

最佳答案

如您所见,这是因为 PSReadline 模块默认安装在 Windows 10 上。您可以使用 Set-PSReadlineKeyHandler 在 PSReadline 中添加自己的 F7。脚本中的 cmdlet。例子:

Set-PSReadlineKeyHandler -Key F7 -BriefDescription "History" -LongDescription "Show command history" -ScriptBlock {
  $pattern = $null
  [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref] $pattern, [ref] $null)
  if ( $pattern ) {
    $pattern = [Regex]::Escape($pattern)
  }
  $history = [System.Collections.ArrayList] @(
    $last = ""
    $lines = ""
    foreach ( $line in [System.IO.File]::ReadLines((Get-PSReadlineOption).HistorySavePath) ) {
      if ( $line.EndsWith('`') ) {
        $line = $line.Substring(0, $line.Length - 1)
        $lines = if ( $lines ) { "$lines`n$line" } else { $line }
        continue
      }
      if ( $lines ) {
        $line = "$lines`n$line"
        $lines = ""
      }
      if ( ($line -cne $last) -and ((-not $pattern) -or ($line -match $pattern)) ) {
        $last = $line
        $line
      }
    }
  )
  $command = $history | Out-GridView -Title History -PassThru
  if ( $command ) {
    [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
    [Microsoft.PowerShell.PSConsoleReadLine]::Insert(($command -join "`n"))
  }
}

使用此功能时,F7 键将出现在弹出的 GridView 窗口中。选择一个历史条目并按 Enter,PowerShell 会将其放在命令行上进行编辑。

关于powershell - 在 Windows 10 Powershell 中通过 F7 使命令历史弹出窗口工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50376858/

相关文章:

python - Anaconda 3 Spyder 似乎导致 Windows 10 上的互联网中断

cmd - VMware Workstation 和 Device/Credential Guard 不兼容

powershell - 如何创建文件夹、共享和应用NTFS权限

azure - 创建 Azure 存储 Blob 容器时出现错误 403(启用存储防火墙

powershell - 如何在 PowerShell 中退出 ForEach-Object

windows-10 - Windows 10 TTS 语音未显示?

python - 导入错误: No module named inspect

python-3.x - Anaconda 安装时没有 conda.exe

powershell - PowerShell 中的复杂变量字符串

powershell - 为什么在 Powershell 的内置类型上使用 add-member 会发生这种奇怪的行为?