Get-World | ConvertTo-PowerShell
Archive for January, 2012
Auto complete for about_* topics
Jan 7th
Today I was heavily working with PowerShell help system. And was tired to call it using Get-Help cmdlet. So I created small function which I put to my profile:
function Show-HelpAboutFunction { $topics = Get-Help about_* foreach ($t in $topics) { $tn = $t.Name -replace 'about_', 'abt-' $text = "function global:$($tn) { Get-Help $($t.Name) }" Invoke-Expression "$text" } }
So when in console I can write: abt-func<Tab> and can circulate help topics till have the one I need. I know that for some topics is better to write Get-Help with partial help topic name, but in some cases I like my solution more.
Note: I know that abt is not approved verb
ISE: List variables in active Script Pane
Jan 1st
I was working in ISE pretty much during last two days (oh yes, was nice end of 2011 and beginning of 2012). During script creation I was checking values of variables frequently. Wouldn’t be nice to see variables value and not just write it’s name to Command Pane? So I came with another menu Add-on command.
function GetValues { $content = $psISE.CurrentFile.Editor.Text $tokens = [System.Management.Automation.PsParser]::Tokenize($content, [ref] $null) |? {$_.Type -eq 'Variable'} | Sort Content -Unique foreach ($t in $tokens) { $prop = @{ Name = $t.Content Value = Get-Variable -Name $($t.Content) -ValueOnly -ErrorAction SilentlyContinue } New-Object -TypeName PSObject -Property $prop | Select Name, Value } } $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add('Show values',{GetValues},'CTRL+SHIFT+V') | Out-Null
When you work with any script, you can run it and will see all actual script variables in Output Pane.

Using Tokenizer API I received all variables from current script and created new object with variable name and value. Then it’s send to output. I am thinking about some graphical output (WPF or ShowUI) or just to Out-GridView. There are also some another points to update but for my current needs it’s OK.
ISE: Run current line without selecting, AKA v3 feature
Jan 1st
I like one of new PowerShell v3 features. When you are in ISE and want to run current line, just press F8, it’s no more necessary to select the line and then use Run Selection (F8). I’d like to have it in v2 also so I created another quick function:
function RunLine { $editor = $psISE.CurrentFile.Editor $currentLine = $editor.CaretLine $currentLineLength = $editor.GetLineLength($currentLine) $currentPosition = $editor.CaretColumn $psISE.CurrentFile.Editor.Select( $currentLine, 1, $currentLine, $currentLineLength + 1 ) "Running >> $($psISE.CurrentFile.Editor.SelectedText)" Invoke-Expression $psISE.CurrentFile.Editor.SelectedText $psISE.CurrentFile.Editor.Select( $currentLine, $currentPosition, $currentLine, $currentPosition ) } $makovecRoot = $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Makovec", $null, $null) $makovecRoot.Submenus.Add('Run line',{RunLine},'CTRL+F8') | Out-Null
It allows me to be at some line and by pressing Ctrl+F8 just run it:

If you want to see another cool new features in ISE v3, go to Windows Management Framework 3.0 CTP2 page and download document named Windows PowerShell ISE.pdf.
