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)
4 Answers
Not elegant but functional...
To Comment
"Block Select" at the start of all the lines by:
- AltMouse-Left-Click-Dragor
- AltShift while ↑↓
... then ...
- Shift3
To UnComment
"Block Select" all the # characters at start of all the lines by:
- AltMouse-Left-Click-Dragor
- AltShift while ↑↓←→
... then ...
- Delete
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'
1Here are some ways to do that:
Here for multiple lines
Here for single and multiple lines (my choice)
And a detailed one
New solution using ISESteroids
This is a much better solution but it involves installing a plugin for the ISE.
Paste this in the editor:
Install-Module -Name "ISESteroids" -Scope CurrentUser -Repository PSGallery -ForcePaste this to run it.
Start-SteroidsThen select the text and use the keyboard shortcut Ctrl+Shift+B
Now you have this functionality inside the editor.
To auto load ISESteroids
- To have ISESteroids load every time you start PowerShell ISE, click on the icon shown
- Paste this:
Start-Steroids - Save this file and closse.
- 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:
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.