Left-padding in Powershell


/ Published in: Windows PowerShell
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. [string]$stringToPad = 1
  2. $padChar = "X"
  3. $paddedLength = 4
  4. $stringToPad = ($padChar * ($paddedLength - $stringToPad.Length)) + $stringToPad

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.