DCSIMG
Using Help in PowerShell ISE - Shay Levy

Shay Levy

If you repeat it, PowerShell it!

Using Help in PowerShell ISE

Windows PowerShell Integrated Scripting Environment (ISE) comes with a graphical help file (chm) which contains complete help for PowerShell as well as help for using ISE. The help file is located under C:\Windows\Help\Mui\<LCID>.

To invoke help, press F1:

image

ISE’s help is also context-sensitive in terms of cmdlet names. If you press F1 while your mouse cursor is positioned inside a cmdlet name, help is displayed for the corresponding cmdlet:

image

Note: Context sensitive help doesn’t work for aliases, more on this below.

You can also disable local help and force ISE to get help directly from Technet:

$psISE.Options.LocalHelp = $false

Now, verify that the cursor is positioned accordingly and press F1, help is coming in from the internet:

image 


ISE support two types of help: Local and Online, both implemented in two variables: $psLocalHelp and $psOnlineHelp

PS > $psLocalHelp Key Value --- ----- Add-Computer WindowsPowerShellHelp.chm::/html/093f660c-b8d5-43cf-aa0c-54e5e54e76f9.htm Add-Content WindowsPowerShellHelp.chm::/html/0c836a1b-f389-4e9a-9325-0f415686d194.htm Add-History WindowsPowerShellHelp.chm::/html/9c7c19cd-56e8-4fb2-b476-3adc589f7a97.htm Add-Member WindowsPowerShellHelp.chm::/html/568588f6-4fed-47e5-819f-2cd326b71ed4.htm Add-PsSnapin WindowsPowerShellHelp.chm::/html/153a2236-80bd-4f12-9852-411471f92441.htm (...)

 

 

PS > $psOnlineHelp Key Value --- ----- Add-Computer http://go.microsoft.com/fwlink/?LinkID=135194 Add-Content http://go.microsoft.com/fwlink/?LinkID=113278 Add-History http://go.microsoft.com/fwlink/?LinkID=113279 Add-Member http://go.microsoft.com/fwlink/?LinkID=113280 Add-PsSnapin http://go.microsoft.com/fwlink/?LinkID=113281 (...)

Both variables are generic types (System.Management.Automation.Host.ObservableDictionary`2), similar to Hash Tables they have Key/Value pair members, each of which contains a cmdlet name and a pointer to the help page location.

I wanted to mimic the way ISE’s help works when you press F1 and allow an alias to be expanded to cmdlet name. With these functions the cursor position doesn’t have to be inside the name, it can be on either side as well as inside a name/alias, allowing you to get help for one letter long aliases. To use the functions, execute the code in ISE or include them in your ISE profile (you can also download them from the ‘Attachment’ section at the end of this post).

function Get-LocalHelp{
   
  
# check if local help is disabled
   if(!$psIse.Options.LocalHelp)
    {
      
throw 'Local help is disabled. To enable Local help execute:'+"`n"+
          
'PS > $psISE.Options.LocalHelp=$true'
    }
   
  
# get the caret position and current line
   $col = $psIse.CurrentOpenedFile.Editor.CaretColumn
  
$line = $psIse.CurrentOpenedFile.Editor.CaretLine
   
  
# tokenize the active editor script
   $content = $psIse.CurrentOpenedFile.Editor.Text
  
$tokens = [System.Management.Automation.PsParser]::Tokenize($content,[ref]$null)
   
  
#
   $cmd = $tokens |where {$_.StartLine -eq $line `
                          
-AND $_.StartColumn -le $col -AND $_.EndColumn -ge $col }
      
  
if($cmd)
    {   
      
if($psLocalHelp.ContainsKey($cmd.content))
        {
            hh.exe
$psLocalHelpitem($cmd.content)
          
return
        }



      
$alias =  (Get-Alias |where {$_.Name -eq $cmd.content}).Definition
      
if ($alias)
        {
            hh.exe
$psLocalHelp.item($alias)   
        }
    }               

}




function Get-OnlineHelp{

# online help is displayed in default browser

  
$col = $psIse.CurrentOpenedFile.Editor.CaretColumn
  
$line = $psIse.CurrentOpenedFile.Editor.CaretLine
   
  
$content = $psIse.CurrentOpenedFile.Editor.Text
  
$tokens = [System.Management.Automation.PsParser]::Tokenize($content,[ref]$null)
   
  
$cmd = $tokens | where {$_.StartLine -eq $line `
                          
-AND $_.StartColumn -le $col -AND $_.EndColumn -ge $col }
   

  
if($cmd)
    {
      
if($psOnlineHelp.ContainsKey($cmd.content))
        {
            [
Diagnostics.Process]::Start($psOnlineHelp.item($cmd.content))
          
return
        }



      
$alias = (Get-Alias | where {$_.Name -eq $cmd.content}).Definition
      
if ($alias)
        {  
            [
Diagnostics.Process]::Start($psOnlineHelp.item($alias))
        }
    }

}



# register shortcut keys
$null = $psIse.customMenu.subMenus.add("Get-PSLocalHelp",{Get-LocalHelp},"CTRL+F1")
$null = $psIse.customMenu.subMenus.add("Get-PSOnlineHelp",{Get-OnlineHelp},"CTRL+F2")

Code is color coded using PowerGUI. Comments, Suggestions, Improvements are most welcome.

Comments

seajhawk said:

very nice! especially not having to put the cursor inside the alias. does this extend to cmdlets as well?

-chris

# February 1, 2009 12:29 AM

ScriptFanatic said:

Yes it does, give it a try.

# February 1, 2009 12:47 AM

Episode 60 - Scripting UI with Joel Bennett and James Brundage « PowerScripting Podcast said:

Pingback from  Episode 60 - Scripting UI with Joel Bennett and James Brundage &laquo; PowerScripting Podcast

# February 23, 2009 6:09 AM

Windows PowerShell Blog said:

ISE has an F1 help feature, where if you press F1 when your caret is over a cmdlet name, context sensitive

# April 21, 2009 1:12 AM

Adding your own help links to ISE | Coded Style said:

Pingback from  Adding your own help links to ISE | Coded Style

# April 21, 2009 4:03 AM

Windows PowerShell Team Blog said:

ISE has an F1 help feature, where if you press F1 when your caret is over a cmdlet name, context sensitive

# April 21, 2009 4:59 AM

PowerShell Team Blog (external) said:

ISE has an F1 help feature, where if you press F1 when your caret is over a cmdlet name, context sensitive

# April 21, 2009 3:40 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: