PowerTip: Pad String to Left with PowerShell


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

Comments (1)

  1. James A. Brown says:

    That’s not a triangle.

    😉

Skip to main content