Last active
November 10, 2021 08:50
-
-
Save JustinGrote/933f79d2dbb5445ff4193d7dee812489 to your computer and use it in GitHub Desktop.
Press F1 to view the currently typed command help online
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace System.Management.Automation | |
using namespace System.Management.Automation.Language | |
#requires -version 5 | |
# Stolen and modified from https://github.com/PowerShell/PSReadLine/blob/master/PSReadLine/SamplePSReadLineProfile.ps1 | |
# F1 for help on the command line - naturally | |
Set-PSReadLineKeyHandler -Key F1 ` | |
-BriefDescription CommandHelp ` | |
-LongDescription 'Open the help window for the current command' ` | |
-ScriptBlock { | |
param($key, $arg) | |
$ast = $null | |
$tokens = $null | |
$errors = $null | |
$cursor = $null | |
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$ast, [ref]$tokens, [ref]$errors, [ref]$cursor) | |
$commandAst = $ast.FindAll( { | |
$node = $args[0] | |
$node -is [CommandAst] -and | |
$node.Extent.StartOffset -le $cursor -and | |
$node.Extent.EndOffset -ge $cursor | |
}, $true) | Select-Object -Last 1 | |
if ($commandAst -ne $null) { | |
$commandName = $commandAst.GetCommandName() | |
if ($commandName -ne $null) { | |
$command = $ExecutionContext.InvokeCommand.GetCommand($commandName, 'All') | |
if ($command -is [Management.Automation.AliasInfo]) { | |
$commandName = $command.ResolvedCommandName | |
} | |
if ($commandName -ne $null) { | |
#First try online | |
try { | |
Get-Help $commandName -Online -ErrorAction Stop | |
} catch [InvalidOperationException] { | |
if ($PSItem -notmatch 'The online version of this Help topic cannot be displayed') { throw } | |
Get-Help $CommandName -ShowWindow | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment