Padding Strings Left and Right

by Mar 6, 2014

If you must make sure that a given string has a uniform width, then you can use .NET methods to pad the string appropriately:

$mytext = 'Test'

$paddedText = $mytext.PadLeft(15)
"Here is the text: '$paddedText'"

$paddedText = $mytext.PadRight(15)
"Here is the text: '$paddedText'" 

This is the result:

You can even add a padding character yourself (if you do not want to pad with spaces):

Twitter This Tip! ReTweet this Tip!