A common situation I run into when building PowerShell scripts is needing to "pad" zeroes to various stings. String padding means adding characters to either the left or right of a string making the string a certain maximum length.

For example, perhaps I want to represent the number 1 as 0001. I could just add the zeroes statically to make one big string like '{0}{1}' -f '000','1'.

Concatenating strings using string formatting will indeed add some leading zeroes, but it's not dynamic at all. Most of the time in my code, I'm not lucky enough to deal with a single number like 1 in every instance. I want to add a range of zeroes because the number I'm dealing with could be 1, 513, or 2303. I need a way to add the correct number of leading zeroes dynamically.

Perhaps I want to add leading zeroes up to a maximum of two. I could do something crazy like this to get the job done:

if ($number.Length -eq 1) {
    '{0}{1}' -f $number,'00'
} elseif ($number.Length -eq 2) {
    '{0}{1}' -f $number,'0'
} else {

But don't do that. Instead, we can simply use the PadLeft and PadRight string methods to get this done with a whole lot less code.

Every string object has a PadLeft and a PadRight method that allows you to add leading or ending characters to a string. Each method requires the character you'd like to add to the string as well as the maximum length of the string. Using PadLeft, I could, for example, add leading zeroes to an existing string up to a maximum string length of four.

$string = 'foo'
$string =.PadLeft(4,'0')
Adding zeroes to string

Adding zeroes to string

The 4 indicates the maximum length I'd like the string to be, and it just adds as many zeroes as necessary to fill that gap.

When padding numbers on the other hand, we've got to perform an extra step. Since PowerShell is a dynamically typed language, it has a tendency to convert strings to integers and vice versa. PadLeft is a string method, not an integer method. For this reason, to add leading zeroes to a number, we must first cast it to a string to make the PadLeft method available to us, and then we can use the method to make it happen.

PS> $number = 1
PS> ([string]$number).PadLeft(4,'0')

Also notice I've been making the zero a string ('0') all along as well. The PadLeft method only allows you to add string characters.

Finally, let's not forget the PadRight command. We can do the exact same thing, only by adding zeroes to the end of a string rather than to the beginning. In my experience though, this hasn't been nearly as frequent a requirement that padding leading zeroes has been.

Subscribe to 4sysops newsletter!

PS> $number = 1
PS> ([string]$number).PadRight(4,'0')

Use the PadLeft and PadRight methods any time you need to add leading zeroes to your strings!

  1. Ray 5 years ago

    Thanks, this is a great addition to my arsenal. We have some data with leading zeros that this will save massive time.

  2. Ian 3 years ago

    Thanks. This is going to allow me to format variables much better.

  3. Moubai 3 years ago

    So much thanks, you save me hours of work 😉

  4. Dieter 2 years ago

    Many thanks! 

    This is exactly what I have been looking for because formatting after type conversion from int to string is really awkward.

Leave a reply

Your email address will not be published.


© 4sysops 2006 - 2022


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account