{"id":11331,"date":"2012-01-29T00:01:00","date_gmt":"2012-01-29T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/01\/29\/multiply-powershell-strings-to-customize-text-underlines\/"},"modified":"2012-01-29T00:01:00","modified_gmt":"2012-01-29T00:01:00","slug":"multiply-powershell-strings-to-customize-text-underlines","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/multiply-powershell-strings-to-customize-text-underlines\/","title":{"rendered":"Multiply PowerShell Strings to Customize Text Underlines"},"content":{"rendered":"<p><b>Summary<\/b>: Create a Windows PowerShell function that accepts pipelined input and creates a variable length underline that uses various characters.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. One of the things I really enjoy doing is reading the comments that are posted by various people on the Hey, Scripting Guy! Blog. Although I do not respond to every comment, I do read them, and I would like to respond to all the comments, but I have a tendency to get bogged down at times, and it precludes me from responding to comments.<\/p>\n<p>Anyway, a little over a month ago, I wrote a pretty cool blog: <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/12\/25\/use-powershell-and-ascii-to-create-folders-with-letters.aspx\" target=\"_blank\">Use PowerShell and ASCII to Create Folders with Letters<\/a><i>. <\/i>This blog generated a decent number of comments. A comment by JV mentioned that you could multiply letters. This is a cool trick that I have used for years. To multiply strings, you use the multiplication operator shown in the code that follows.<\/p>\n<p style=\"padding-left: 30px\">&ldquo;a&rdquo; * 5<\/p>\n<p>It is also possible to multiply longer strings. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">&#8220;This is a string&#8221; * 2<\/p>\n<p>One of the really cool things that I use the string multiplication trick to do is to create an underline that is exactly the same length as the string it highlights. To do this, I use the <b>Length<\/b><i> <\/i>property of the string and supply that to the multiplication operator along with the desired line separator to use. In the code that follows, I assign a string to the variable <b>$a<\/b>. Next, I use the <b>Length<\/b><i> <\/i>property, which is a property that always exists on <b>System.String<\/b><i> <\/i>objects, to determine the length of the string. I use the length of the string to determine how many times I want to multiply the underscore character (&ldquo;_&rdquo;). Next I display the string, and finally, I display the newly created underline. The code is shown here.<\/p>\n<p style=\"padding-left: 30px\">$a = &#8220;this is a string&#8221;<\/p>\n<p style=\"padding-left: 30px\">$b = &#8220;_&#8221; * $a.length<\/p>\n<p style=\"padding-left: 30px\">$a<\/p>\n<p style=\"padding-left: 30px\">$b<\/p>\n<p>As a further test of this technique, I create a longer string, calculate the length of the new string, create a new underline character, and once again display the output. This is shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5468.HSG-01-29-12-01.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5468.HSG-01-29-12-01.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>When you know that you can multiply strings and use the technique to create a custom-sized underline, the next step is to turn it into a simple function. The complete <b>New-Underline<\/b> function appears here.<\/p>\n<p style=\"padding-left: 30px\">Function New-Underline<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;[CmdletBinding()]<\/p>\n<p style=\"padding-left: 30px\">param(<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [Parameter(Mandatory = $true,Position = 0,valueFromPipeline=$true)]<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [string]<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $stringIN,<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [string]<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $char = &#8220;_&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;)<\/p>\n<p style=\"padding-left: 30px\">&nbsp; $underLine= $char * $stringIn.length<\/p>\n<p style=\"padding-left: 30px\">&nbsp; $stringIn<\/p>\n<p style=\"padding-left: 30px\">&nbsp; $underLine<\/p>\n<p style=\"padding-left: 30px\">} #end function new-underline<\/p>\n<p>The first thing I do is tell the function to use the <b>CmdletBinding<\/b><i> <\/i>attribute, this tells the function to behave as if it was a cmdlet when processing parameters, and excess arguments passed to the function that do not have defined parameters generate an error. Next, to create the input parameters, I use the <b>Param<\/b><i> <\/i>keyword. I then specify parameter attributes to make the first parameter mandatory, identify it as position 0, and accept values passed along from the pipeline. These first few lines of code are shown here.<\/p>\n<p style=\"padding-left: 30px\">Function New-Underline<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;[CmdletBinding()]<\/p>\n<p style=\"padding-left: 30px\">param(<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [Parameter(Mandatory = $true,Position = 0,valueFromPipeline=$true)]<\/p>\n<p>Next, the <b>$stringIn<\/b> variable is specified to be a string, as is the <b>$char<\/b> variable. The <b>$stringIn<\/b> variable holds the input that is passed to the function (the input to be underlined), and the <b>$char<\/b> variable holds the character to use for the underlining. The <b>$char<\/b> variable is assigned a default value of &ldquo;_&rdquo;. This portion of the function is shown here.<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [string]<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $stringIN,<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [string]<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $char = &#8220;_&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;)<\/p>\n<p>The main part of the function uses the string multiplication technique mentioned earlier. The code retrieves the value to use for the underline from the <b>$char<\/b> variable and multiplies it by the length of the string. The <b>$underline<\/b> variable stores the newly created underline. Next the values contained in the <b>$stringIn<\/b> and the <b>$underLine<\/b> variables return from the function. This portion of the function is shown here.<\/p>\n<p style=\"padding-left: 30px\">&nbsp; $underLine= $char * $stringIn.length<\/p>\n<p style=\"padding-left: 30px\">&nbsp; $stringIn<\/p>\n<p style=\"padding-left: 30px\">&nbsp; $underLine<\/p>\n<p style=\"padding-left: 30px\">} #end function new-underline<\/p>\n<p>I obtain the full path to the script that contains the <b>New-UnderLine<\/b> function by using the Windows PowerShell ISE object model. Here is the code I used to easily retrieve the path.<\/p>\n<p style=\"padding-left: 30px\">$psise.CurrentFile.FullPath<\/p>\n<p>I copy and paste that into the Windows PowerShell console, and then I use dot-sourcing to bring the function into the current Windows PowerShell environment. Here is the code I used to do that (the period is the first character on the line, followed by a space, then the path to the script).<\/p>\n<p style=\"padding-left: 30px\">. E:\\data\\ScriptingGuys\\2012\\HSG_1_23_12\\new-underline.ps1<\/p>\n<p>I then test the function to ensure that it works properly. The image that follows shows that the function works great.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8877.HSG-01-29-12-02.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8877.HSG-01-29-12-02.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Well, that is about all for now. I hope you enjoy the rest of your weekend. Keep on scripting!<\/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><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Create a Windows PowerShell function that accepts pipelined input and creates a variable length underline that uses various characters. Microsoft Scripting Guy, Ed Wilson, is here. One of the things I really enjoy doing is reading the comments that are posted by various people on the Hey, Scripting Guy! Blog. Although I do not [&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,21,61,45],"class_list":["post-11331","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-string-manipulation","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Create a Windows PowerShell function that accepts pipelined input and creates a variable length underline that uses various characters. Microsoft Scripting Guy, Ed Wilson, is here. One of the things I really enjoy doing is reading the comments that are posted by various people on the Hey, Scripting Guy! Blog. Although I do not [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11331","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=11331"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11331\/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=11331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=11331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=11331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}