PowerShell_ISE Scripting: ConvertTo-Comment

PowerShell Team

Let me combine a couple of things. 

1) When I posted my PowerShell_ISE profile, I included a function Goto-Line that is now builtin to the ISE so I reposted the blog with this function commented out. I commented it out instead of deleting it because I wanted to provide an example of how to do it.

2) Vivek Sharma left a comment asked about programmatically controlling indentation so I gave a couple of pointers to the objects required to program the editor.

So let’s take those 2 and put them together and write a function which will convert the selected text into a PowerShell comment (using another CTP3 feature – block comments”). 

Here goes:

function ConvertTo-Comment
{
    $editor = $psISE.CurrentOpenedFile.Editor
    $CommentedText = “<#`n” + $editor.SelectedText + “#>”
    # INSERTING overwrites the SELECTED text
    $editor.InsertText($CommentedText)
}
$null = $psISE.CustomMenu.Submenus.Add(“Comment Selected”, {ConvertTo-Comment}, $null)

 

Give that a try and then start experimenting on your own.  Think TECOTPU, Emacs etc.  Go crazy and share share share.

Enjoy!

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

0 comments

Discussion is closed.

Feedback usabilla icon