Adding Single Line Comments in PowerShell ISE

by Nov 29, 2017

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!