In the previous tip we looked at command extensions for the good old PowerShell ISE. Here is another sample that adds the CTRL+K keyboard shortcut to append comments (“#”) to each selected line:

function Invoke-Comment
{
    $file = $psise.CurrentFile                              
    $comment = ($file.Editor.SelectedText -split '\n' | ForEach-Object { "#$_" }) -join "`n"                                                 
    $file.Editor.InsertText($comment)                     
}

$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add('Comment Out', { Invoke-Comment }, 'CTRL+K')

Twitter This Tip! ReTweet this Tip!

Anonymous
  • I use something similar in my ISE profile, and I have the reverse as well; function Remove-Comment { $editor = $psISE.CurrentFile.Editor $editor.InsertText(($editor.SelectedText.Split("`n") | ForEach-Object { $_ -replace "^#","" }) -join "`n") }