Multiply PowerShell Strings to Customize Text Underlines

Multiply PowerShell Strings to Customize Text Underlines

  • Comments 1
  • Likes

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 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.

Anyway, a little over a month ago, I wrote a pretty cool blog: Use PowerShell and ASCII to Create Folders with Letters. 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.

“a” * 5

It is also possible to multiply longer strings. This technique is shown here.

"This is a string" * 2

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 Length 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 $a. Next, I use the Length property, which is a property that always exists on System.String 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 (“_”). Next I display the string, and finally, I display the newly created underline. The code is shown here.

$a = "this is a string"

$b = "_" * $a.length

$a

$b

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.

Image of command output

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 New-Underline function appears here.

Function New-Underline

{

 [CmdletBinding()]

param(

      [Parameter(Mandatory = $true,Position = 0,valueFromPipeline=$true)]

      [string]

      $stringIN,

      [string]

      $char = "_"

 )

  $underLine= $char * $stringIn.length

  $stringIn

  $underLine

} #end function new-underline

The first thing I do is tell the function to use the CmdletBinding 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 Param 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.

Function New-Underline

{

 [CmdletBinding()]

param(

      [Parameter(Mandatory = $true,Position = 0,valueFromPipeline=$true)]

Next, the $stringIn variable is specified to be a string, as is the $char variable. The $stringIn variable holds the input that is passed to the function (the input to be underlined), and the $char variable holds the character to use for the underlining. The $char variable is assigned a default value of “_”. This portion of the function is shown here.

      [string]

      $stringIN,

      [string]

      $char = "_"

 )

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 $char variable and multiplies it by the length of the string. The $underline variable stores the newly created underline. Next the values contained in the $stringIn and the $underLine variables return from the function. This portion of the function is shown here.

  $underLine= $char * $stringIn.length

  $stringIn

  $underLine

} #end function new-underline

I obtain the full path to the script that contains the New-UnderLine function by using the Windows PowerShell ISE object model. Here is the code I used to easily retrieve the path.

$psise.CurrentFile.FullPath

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).

. E:\data\ScriptingGuys\2012\HSG_1_23_12\new-underline.ps1

I then test the function to ensure that it works properly. The image that follows shows that the function works great.

Image of command output

Well, that is about all for now. I hope you enjoy the rest of your weekend. Keep on scripting!

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
  • Hi Ed,

    this is a somehow wonderful example of what we cculd do (easily) with Powershell,

    but imho. of something we might never have to do :-)

    If you ask me, I won't find anybody to envy my underline capabilities if they are restricted to simple ASCII text. Nobody seems to love this anymore ... years ago, as we did output plenty thousand of pages of printed Logs on green paper with red lines in between ... we would have been kings to have that function!

    But now ... no chances to win a dime any more!

    Anyway : the function is a nice example of what we can easily do with PS and reading through your article saved my day ... I did all this stuff years ago with algol 67 and Fortan IV or 77 eg.

    Long ago and far more complicated ...

    Good times !?

    Well ... yes and no!

    Klaus (Schulte)