Weekend Scripter: Automatically Indent Your PowerShell Code in the ISE

Weekend Scripter: Automatically Indent Your PowerShell Code in the ISE

  • Comments 1
  • Likes

Summary: Microsoft Scripting Guy, Ed Wilson, shows how to create a function to indent your code in the Windows PowerShell ISE without tabbing.

Microsoft Scripting Guy, Ed Wilson, is here. One of the cool things about a road trip is that it gives me a chance to catch up on stuff I want to do. Luckily, the Scripting Wife enjoys driving, and I enjoy writing Windows PowerShell code while she is driving, so it works great (way better than me trying to drive from the passenger’s seat).

One of the things I have wanted to do for a long time is write a function that will automatically indent selected code inside the Windows PowerShell ISE. Of course, I could highlight the code and press Tab. The problem with that is that I get an invisible tab character (“`t”) in my code. For most people, this is a non-issue, but some of the applications that I use (for writing books, for example) do not like Tabs in code blocks. Some script editors have an option where you can set Use spaces instead of tabs, but the Windows PowerShell ISE does not have this feature—until now. And, while I was at it, I decided to do something that even other script editors don’t do, and that is, I made it variable. So I can specify indented code for one section of 3 spaces—and later on, I can indent another section 6 spaces—and later, another section, 9 spaces—or whatever I want to do.

The Move-Text function

The Move-Text function accepts a single input parameter; that is, an integer for how many spaces to indent the selected code. I set a default value of 1, mostly to avoid errors, but also because most of the time, I only indent my code a single space. So this saves me time. If you would like to make the default 2 or 3, you can edit this line:

Param([int]$space = 1)

The next thing I need to do is to create the space that I will use for my “tab” stop. To do this, I multiply a blank space by the number defined in the $space variable. This line of code is shown here.

$tab = " " * $space

Next, I pick up the selected text and assign it to the $text variable as shown here.

$text = $psISE.CurrentFile.editor.selectedText

Now I use the ForEach statement to walk through the selected text. I need to split the selected text on the Newline character so that I have lines instead of characters of selected text. This is shown here.

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

Inside the script block, I create new text and assign it to the $newtext variable. I take the spaces stored in the $tab variable and add the line of code to it. I then append a Newline character to the selected code. This is shown here.

{

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

  }

Finally, I use the InsertText method to insert the newly created text with the indented code into the script editor. This code accomplishes that task.

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

The complete Move-Text function is shown here.

Move-Text Function

function move-text

{

  <#

   .Synopsis

    This function will indent text in the ISE a specific number

   .Description

    This function will indent selected text in the PowerShell ISE. These are

    real spaces, not tabs. Therefore this is appropriate for situations where

    an actual tab "`t" will not work.

   .Example

    move-text -space 5

    moves selected text five spaces

   .Parameter spaces

    The number of spaces to indent the selected text. Note this number cannot

    be a negative number, and this function does not "unindent" the selected text.

   .Notes

    NAME:  Move-text

    AUTHOR: ed wilson, msft

    LASTEDIT: 06/11/2012 17:16:29

    KEYWORDS: Windows PowerShell ISE, Scripting Techniques

    HSG:

   .Link

     Http://www.ScriptingGuys.com

 #Requires -Version 2.0

 #>

 Param([int]$space=1)

 $tab = " " * $space

 $text = $psISE.CurrentFile.editor.selectedText

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

  {

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

  }

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

} #end function move-text

The following image illustrates calling the Move-Text function, and it shows indenting the selected text.

Image of command output

Well, that is about it. I added the function to my Windows PowerShell ISE profile module. Now I can easily indent my script without the requirement of incorporating tabs into my code.

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 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment