PowerTip: Pad String to Left with PowerShell

Doctor Scripto

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)


1 comment

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

Feedback usabilla icon