Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 34890

Weekend Scripter: Add Power and Functionality to the PowerShell ISE Part 1

$
0
0

Summary: Microsoft Scripting Guy, Ed Wilson, begins a revision of his Windows PowerShell ISE Module by adding five new functions.

Microsoft Scripting Guy, Ed Wilson, is here. I decided to block off the weekend to work on my Windows PowerShell ISE module. There are several things that I want to add to it because the “annoyance” factor has finally caught up with me. In fact my criteria, when a repetitive task gets to the point that it annoys me, I will write a bit of code and solve the annoying problem. I have a list of five functions that I want to write and add to my Windows PowerShell ISE module. Here are the functions I want to add:

  • Add-RemarkedText
  • Remove-MarkedText
  • Edit-Module
  • Import-EveryModule
  • Switch-OutLineView

So after I make a pot of Gunpowder Green Tea with jasmine, lemon grass, and a half spoonful of spearmint, I head back to my office, and my laptop is busy blaring Willie Nelson.  I look over at the Scripting Wife and say, “I thought I was playing Mott the Hoople when I left. What happened?”

The Scripting Wife said, “You shouldn’t have left your computer unattended.” I guess she has a point, after all. Today I am going to talk about the first two functions: Add-RemarkedText and Remove-MarkedText. Tomorrow I will talk about the other three functions.

Comment (remark) out a block of selected script code

When I have a block of code that I want to comment (or remark) out, I generally use the Windows PowerShell 2.0 block quote feature of the <# to open the comment and #> to close the comment.  This technique is shown here.

<#

"this is my code"

"that I want to comment (remark) out"

"for now"

#>

The problem with this is that one way I troubleshoot my scripts is by commenting out sections of my code. In the Windows PowerShell ISE, I would like to be able to use the mouse (or keyboard shortcuts) to select a block of code and then to comment it out. Using the <# and #> seems to be a bit cumbersome for me.

Note  If I am going to make major changes to a script, I use my Copy-ScriptToNewTab function, which is already in my Windows PowerShell ISE module.

My solution for this dilemma is my new Add-RemarkedText function. To use it, I simply select a portion of the code in the current tab in the Windows PowerShell ISE, and in the command pane, I type Add-RemarkedText. This is shown in the image that follows.

Image of command output

When the Add-RemarkedText function runs, the selected code is now commented (remarked) out, and it will not execute when the script runs. The commented section is shown in the image that follows.

Image of command output

Here is the complete Add-RemarkedText function.

ADD-RemarkedText function

Function Add-RemarkedText

{

 <#

   .Synopsis

    This function will add a remark # character to beginning of line

   .Description

    This function will add a remark character # to selected text in the ISE.

    These are comment characters, and is great when you want to comment out

    a section of PowerShell code.

   .Example

    Add-RemarkedText

    adds the comment / remark character to beginning of each selected line

   .Notes

    NAME:  Add-RemarkedText

    AUTHOR: ed wilson, msft

    LASTEDIT: 05/16/2013

    KEYWORDS: Windows PowerShell ISE, Scripting Techniques

    HSG: wes-5-18-13

   .Link

     Http://www.ScriptingGuys.com

 #Requires -Version 2.0

 #>

 $text = $psISE.CurrentFile.editor.selectedText

 foreach ($l in $text -split [environment]::newline)

  {

   $newText += "{0}{1}" -f ("#" + $l),[environment]::newline

  }

   $psISE.CurrentFile.Editor.InsertText($newText)

} #End function add-remarkedtext

Removing a commented section of code

After I have remarked out a portion of code, and I have completed my troubleshooting/testing scenario, it is time to remove the comments. In the past, this meant using the down arrow and the delete key, and hoping the mind numbing repetition did not lull me to sleep and cause me to delete a critical portion of the script.

With my Remove-RemarkedText function, this is no longer a problem. First I highlight (select) the commented out portion of the script, and then in the execution pane of the Windows PowerShell ISE, I type Remove-RemarkedText (rr is an alias). This technique is shown in the following image.

Image of command output

When I press ENTER, the comment character (remarked out code) is removed as shown here:

Image of command output

Keep in mind that the code is still selected, and you should click away to remove the selection to protect yourself from accidently deleting the section of your script.

Here is the complete text of the Remove-RemarkedText function.

Remove-RemarkedText

Function Remove-RemarkedText

{

 <#

   .Synopsis

    This function will remove a remark # character to beginning of line

   .Description

    This function will remove a remark character # to selected text in the ISE.

    These are comment characters, and is great when you want to clean up a

    previously commentted out section of PowerShell code.

   .Example

    Remove-RemarkedText

    Removes the comment / remark character to beginning of each selected line

   .Notes

    NAME:  Add-RemarkedText

    AUTHOR: ed wilson, msft

    LASTEDIT: 05/16/2013

    KEYWORDS: Windows PowerShell ISE, Scripting Techniques

    HSG: wes-5-18-13

   .Link

     Http://www.ScriptingGuys.com

 #Requires -Version 2.0

 #>

 $text = $psISE.CurrentFile.editor.selectedText

 foreach ($l in $text -split [environment]::newline)

  {

   $newText += "{0}{1}" -f ($l -replace '#',''),[environment]::newline

  }

   $psISE.CurrentFile.Editor.InsertText($newText)

} #End function remove-remarkedtext

Join me tomorrow when I will complete my discussion of the other three new ISE functions for my Windows PowerShell ISE module. Tomorrow I will also share the link to the Windows PowerShell ISE module project in the Scripting Guys Script Repository.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 


Viewing all articles
Browse latest Browse all 34890

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>