Get-World | ConvertTo-PowerShell
Posts tagged ISE
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.
ISE: Add new line below/above current
Dec 28th
Today I was playing with PowerShell v3 and was frequently working inside ISE (love it’s Intellisense feature). I was in this situation few times:

As you can see, cursor is at position 0. At this time I want to add new line, so it means go to end of current line and press Enter. I wanted to automate it. Fortunately ISE object model is self-descriptive, so I created this small Add-on:
$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(
'Insert line below',
{
$psise.CurrentFile.Editor.Select(
$psise.CurrentFile.Editor.CaretLine,
$psise.CurrentFile.Editor.GetLineLength($psise.CurrentFile.Editor.CaretLine)+1,
$psise.CurrentFile.Editor.CaretLine,
$psise.CurrentFile.Editor.GetLineLength($psise.CurrentFile.Editor.CaretLine)+1)
$psise.CurrentFile.Editor.InsertText("`n")
}, "CTRL+ENTER")
$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(
'Shift line',
{
$psise.CurrentFile.Editor.Select(
$psise.CurrentFile.Editor.CaretLine, 1,
$psise.CurrentFile.Editor.CaretLine, 1)
$psise.CurrentFile.Editor.InsertText("`n")
$psise.CurrentFile.Editor.Select(
$psise.CurrentFile.Editor.CaretLine-1, 1,
$psise.CurrentFile.Editor.CaretLine-1, 1)
}, "CTRL+SHIFT+ENTER")
I added two features: add line below and before (shift line). You can call it either from menu:

or via keyboard shortcuts.
