May 26th, 2015

PowerTip: Pad String to Left with PowerShell

Doctor Scripto
Scripter

Summary: Use Windows PowerShell to pad a string to the left.

Hey, Scripting Guy! Question How can I use a specific Unicode character with Windows PowerShell to pad a string to the left so that the entire
           string is a certain length?

Hey, Scripting Guy! Answer Use the PadLeft method from the String class. Specify the length of the newly created string and the Unicode
           character as the second parameter. Here is an example that uses a triangle as the pad character. The string is
           6 characters long, and the newly created string is 15 characters long. The padding will be 9 triangles.

PS C:\> $a = "string"

PS C:\> $a.PadLeft(15,[char]4)

♦♦♦♦♦♦♦♦♦string

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

1 comment

Discussion is closed. Login to edit/delete existing comments.