{"id":9021,"date":"2012-06-17T00:01:00","date_gmt":"2012-06-17T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/06\/17\/weekend-scripter-automatically-indent-your-powershell-code-in-the-ise\/"},"modified":"2012-06-17T00:01:00","modified_gmt":"2012-06-17T00:01:00","slug":"weekend-scripter-automatically-indent-your-powershell-code-in-the-ise","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-automatically-indent-your-powershell-code-in-the-ise\/","title":{"rendered":"Weekend Scripter: Automatically Indent Your PowerShell Code in the ISE"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, shows how to create a function to indent your code in the Windows PowerShell ISE without tabbing.<\/p>\n<p>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&rsquo;s seat).<\/p>\n<p>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 (&ldquo;`t&rdquo;) 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 <b>Use spaces instead of tabs<\/b>, but the Windows PowerShell ISE does not have this feature&mdash;until now. And, while I was at it, I decided to do something that even other script editors don&rsquo;t do, and that is, I made it variable. So I can specify indented code for one section of 3 spaces&mdash;and later on, I can indent another section 6 spaces&mdash;and later, another section, 9 spaces&mdash;or whatever I want to do.<\/p>\n<h2>The Move-Text function<\/h2>\n<p>The <b>Move-Text<\/b> 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:<\/p>\n<p style=\"padding-left: 30px\">Param([int]$space = 1)<\/p>\n<p>The next thing I need to do is to create the space that I will use for my &ldquo;tab&rdquo; stop. To do this, I multiply a blank space by the number defined in the <b>$space<\/b> variable. This line of code is shown here.<\/p>\n<p style=\"padding-left: 30px\">$tab = &#8221; &#8221; * $space<\/p>\n<p>Next, I pick up the selected text and assign it to the <b>$text<\/b> variable as shown here.<\/p>\n<p style=\"padding-left: 30px\">$text = $psISE.CurrentFile.editor.selectedText<\/p>\n<p>Now I use the <b>ForEach<\/b><i> <\/i>statement to walk through the selected text. I need to split the selected text on the <b>Newline<\/b> character so that I have lines instead of characters of selected text. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">foreach ($l in $text -split [environment]::newline)<\/p>\n<p>Inside the script block, I create new text and assign it to the <b>$newtext<\/b> variable. I take the spaces stored in the <b>$tab<\/b> variable and add the line of code to it. I then append a <b>Newline<\/b> character to the selected code. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; $newText += &#8220;{0}{1}&#8221; -f ($tab + $l),[environment]::newline<\/p>\n<p style=\"padding-left: 30px\">&nbsp; }<\/p>\n<p>Finally, I use the <b>InsertText<\/b> method to insert the newly created text with the indented code into the script editor. This code accomplishes that task.<\/p>\n<p style=\"padding-left: 30px\">$psISE.CurrentFile.Editor.InsertText($newText)<\/p>\n<p>The complete Move-Text function is shown here.<\/p>\n<p style=\"padding-left: 30px\"><b>Move-Text Function<\/b><\/p>\n<p style=\"padding-left: 30px\">function move-text<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp; &lt;#<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Synopsis<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; This function will indent text in the ISE a specific number<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Description<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; This function will indent selected text in the PowerShell ISE. These are<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; real spaces, not tabs. Therefore this is appropriate for situations where<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; an actual tab &#8220;`t&#8221; will not work.<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Example<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; move-text -space 5<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; moves selected text five spaces<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Parameter spaces<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; The number of spaces to indent the selected text. Note this number cannot<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; be a negative number, and this function does not &#8220;unindent&#8221; the selected text.<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Notes<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; NAME:&nbsp; Move-text<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; AUTHOR: ed wilson, msft<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; LASTEDIT: 06\/11\/2012 17:16:29<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; KEYWORDS: Windows PowerShell ISE, Scripting Techniques<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; HSG:<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Link<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; Http:\/\/www.ScriptingGuys.com<\/p>\n<p style=\"padding-left: 30px\">&nbsp;#Requires -Version 2.0<\/p>\n<p style=\"padding-left: 30px\">&nbsp;#&gt;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;Param([int]$space=1)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$tab = &#8221; &#8221; * $space<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$text = $psISE.CurrentFile.editor.selectedText<\/p>\n<p style=\"padding-left: 30px\">foreach ($l in $text -split [environment]::newline)<\/p>\n<p style=\"padding-left: 30px\">&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; $newText += &#8220;{0}{1}&#8221; -f ($tab + $l),[environment]::newline<\/p>\n<p style=\"padding-left: 30px\">&nbsp; }<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; $psISE.CurrentFile.Editor.InsertText($newText)<\/p>\n<p style=\"padding-left: 30px\">} #end function move-text<\/p>\n<p>The following image illustrates calling the <b>Move-Text<\/b> function, and it shows indenting the selected text.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6320.wes-6-17-12-01.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6320.wes-6-17-12-01.png\" \/><\/a><\/p>\n<p>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.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,61,45,100],"class_list":["post-9021","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell","tag-windows-powershell-ise"],"acf":[],"blog_post_summary":"<p>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. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=9021"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9021\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=9021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=9021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=9021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}