12

In PowerShell ISE I would like to comment out a line or multiple lines at once with a keyboard shortcut like how Sublime Text does this.

Is this possible to add or remove the # shown in the example below via a keyboard shortcut?

[console]::beep(350,400) < toggle betweeen these > #[console]::beep(350,400)

Ste
  • 1,352

6 Answers6

11

Not elegant but functional...

To Comment

"Block Select" at the start of all the lines by:

  • AltMouse-Left-Click-Drag or
  • AltShift while

... then ...

  • Shift3

To UnComment

"Block Select" all the # characters at start of all the lines by:

  • AltMouse-Left-Click-Drag or
  • AltShift while

... then ...

  • Delete
3

I found a solution I like at https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/toggling-comments-in-powershell-ise.

It works for one or many lines, or even within lines. Place this function in your $Profile file.

function Toggle-Comment
{
    $file = $psise.CurrentFile                              
    $text = $file.Editor.SelectedText   
    if ($text.StartsWith("<#")) {
        $comment = $text.Substring(2).TrimEnd("#>") 
    }
    else
    {                            
        $comment = "<#" + $text + "#>"    
    }
    $file.Editor.InsertText($comment)                     
}

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

$Profile is run every time ISE is opened, so the function is always available. The function creates a new menu item with keyboard shortcut Ctrl-K that comments or uncomments the selected lines.

If the $Profile file is not yet created (it's often not), you can create it like this:

New-Item -Path $profile -ItemType "file" -Force

(source for command: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-item?view=powershell-7.2)

asmedley
  • 56
  • 3
2

Old, but I just found this question and found this solution.

  • Select all the lines you want to comment
  • Ctrl-H to Replace in Script
  • Type ^ in 'Find what'
  • Type # in 'Replace with'
  • Check the Regular Expression check box
  • Replace All

Then in reverse use # in 'Find what' and nothing in 'Replace with'

1

Might be easiest to do this with the BoxSelect Feature in ISE.

In the editor, press alt+shift together to select the line at the mouse cursor position. Then press up or down to add the same position to the line above or below the first line to the selection.

Example:

Line 1
Line 2
Line 3

If I place my cursor before line 1, press Alt+Shift and down arrow twice, I would select the same position on all 3 lines. I could then comment out all 3 lines at once by pressing # to get:

#Line 1
#Line 2
#Line 3

Or I could press TAB instead of # to move all 3 lines at the same time:

    Line 1
    Line 2
    Line 3

Alt+Shift and arrow key left or right will select portions of the 3 lines to copy, paste from clipboard or delete.

0

Here are some ways to do that:

Here for multiple lines

Here for single and multiple lines (my choice)

And a detailed one

Timo
  • 150
-1

New solution using ISESteroids

This is a much better solution but it involves installing a plugin for the ISE.

  1. Paste this in the editor: Install-Module -Name "ISESteroids" -Scope CurrentUser -Repository PSGallery -Force

  2. Paste this to run it. Start-Steroids

  3. Then select the text and use the keyboard shortcut Ctrl+Shift+B

Now you have this functionality inside the editor.

To auto load ISESteroids

  1. To have ISESteroids load every time you start PowerShell ISE, click on the icon shown
  1. Paste this: Start-Steroids
  2. Save this file and closse.
  3. Next time you open PowerShell ISE, this plugin will load.

Older defunct answer

This AFAIK cannot be done in the native editor but it can with:

https://code.visualstudio.com/

And by installing an extension called PowerShell by using the Ctrl + Shift + X and searching for PowerShell and installing.

This will allow debugging and syntax highlighting and also has the keyboard shortcuts for blocking out comments Like so:

  • Ctrl + / for toggling a line comment

  • Ctrl + Shift + A for to toggling block comments.

Ste
  • 1,352