Posted By

ericmorin21 on 04/17/09


Pad padding left-padding

Versions (?)

Who likes this?

1 person have marked this snippet as a favorite


Left-padding in Powershell

 / Published in: Windows PowerShell

For clarity, I have broken two variables out, but this could easily be a one-liner. Usage: Replace X with the character you wish to use to pad (e.g. 0 (zero)). Replace Y with an integer representing the desired TOTAL length of the string.

  1. [string]$stringToPad = 1
  2. $padChar = "X"
  3. $paddedLength = 4
  4. $stringToPad = ($padChar * ($paddedLength - $stringToPad.Length)) + $stringToPad

Report this snippet  


RSS Icon Subscribe to comments
Posted By: jmc5113 on June 29, 2013

This also works and is a little cleaner

$Message.PadRight(40, ".")

Posted By: saame1213 on September 30, 2018

I think this is better code Customer conversion

Posted By: rocopoly01 on February 16, 2019

Thanks for the clarification. Very helpful. Trying to implement the same code on our Prepaid Card project site.

You need to login to post a comment.