Format Leading Zeros in PowerShell

I’ve been working on a question in the forums at and the need arose to create a folder name with a 4 digit number. But the number needed to have enough leading zeros so that the number was always 4 digits. For example, Test_0005 or Test_0456. The solution is to use the -f Format operator.

To use this operator on the left side of the statement is a formatting directive and on the right side of -f operator is the value to be formatted. To get the leading zeros I’m going to use a formatting string like this:


This basically says, “take a value and make pad it with 4 0s. Here are some examples:

PS C:\> "{0:0000}" -f 4
PS C:\> "{0:0000}" -f 45
PS C:\> "{0:0000}" -f 456
PS C:\> "{0:0000}" -f 4567

Technically, these are string objects, but that’s ok. Let’s see how we might use this.

PS C:\> 1..10 | foreach {
>> $i="{0:0000}" -f $_
>> $dir="c:\test\Target_$i"
>> $file="file_$i.txt"
>> $target=Join-Path -Path $dir -ChildPath $file
>> Write-Host "Updating $target"
>> }
Updating c:\test\Target_0001\file_0001.txt
Updating c:\test\Target_0002\file_0002.txt
Updating c:\test\Target_0003\file_0003.txt
Updating c:\test\Target_0004\file_0004.txt
Updating c:\test\Target_0005\file_0005.txt
Updating c:\test\Target_0006\file_0006.txt
Updating c:\test\Target_0007\file_0007.txt
Updating c:\test\Target_0008\file_0008.txt
Updating c:\test\Target_0009\file_0009.txt
Updating c:\test\Target_0010\file_0010.txt
PS C:\>

Within the For loop I’m taking the piped in number and formatting to 4 places with leading zeros. This value is then inserted into variables for paths and files, using Join-Path to combine them. This is a much better technique than trying to concatenate.

I hope you find this useful.

5 thoughts on “Format Leading Zeros in PowerShell

  1. Jeff – you are wastiing zeros.

    ‘{0:d4} {1:d4} {2:d4} {3:d4}’ -f 3,33,333,3333

    The formatter has field width specifier which is her set to 4. Isn’t this easier that counting zeros. Similarly we can justify left and right in a given field width.

    ‘|{0,10:d4}|{0,-10:d4}|{0,-20:d4}|{0,20:d4}’ -f 3

    The pipes delimit the fields.

    heX marks the spot:
    ‘|0x{0,4:x4} |0x{0,8:x8} |0x{0,16:x16} |0x{0,32:x32}’ -f -1

    Which allows us to discover some interesting number qualities in a processor.
    ‘|0x{0,4:x4} |0x{0,8:x8} |0x{0,16:x16} |0x{0,32:x32}’ -f ([int64]-1)

    The little ‘ohs’ are all really zeros however teh web page typeface may mske them look like ohs.

    • I was just getting ready to post a comment. Yes, this works much easier.

      PS C:\> “{0:d4}” -f 45
      PS C:\> “{0:d4}” -f 450
      PS C:\> “{0:d4}” -f 4567

  2. I posted it because I really like teh C#/NET formatter in POwerShell. I am glad to see you are adding this to your repetoire.

    Good article.

    • The -f operator is very cool, but doesn’t get enough attention in PowerShell. It is also not easy to use or discover for IT Pros. You have to know it exists and wade through MSDN docs to figure out how to use it.

Comments are closed.