8

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,166
  • 2
  • 9
  • 23

5 Answers5

5

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
George 2.0 Hope
  • 544
  • 5
  • 12
  • Please explain how this is any different from `put the cursor where you want the "#" and then type "#"`, and i am including the multi-select in my question. – Max Cascone May 26 '21 at 17:24
  • Because you only have to type the "#" character once, vs typing it for every line. – George 2.0 Hope Oct 28 '21 at 16:38
  • if you can multi-select then you only have to type it once, too. – Max Cascone Oct 28 '21 at 16:50
  • In that narrow regard, I suppose it's not any different. I was addressing more of the 'spirit' of the question... i.e... how do you quickly comment/uncomment some code. And while @Timo's answer may or may not link successfully to what I have, it's not really the SO "way"... so I included the answer directly within my post. – George 2.0 Hope Oct 28 '21 at 18:43
  • 1
    I love this solution! Thanks – EDM May 09 '23 at 03:02
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
  • 46
  • 3
  • 1
    Thanks for that. You can see the path of the profile with this command in PS. `$profile`. When you can the path create a file called `Microsoft.PowerShellISE_profile.ps1` in that path and use your code. Thanks again. Great first answer. – Ste Jul 08 '22 at 20:01
  • Thanks! I'm glad you added that. – asmedley Apr 04 '23 at 16:07
1

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'

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
  • 148
  • 10
-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,166
  • 2
  • 9
  • 23