Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Format Leading Zeros in PowerShell

Posted on January 3, 2012February 16, 2021

I've been working on a question in the forums at ScriptingAnswers.com 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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

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

 "{0:0000}"

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

PS C:\> "{0:0000}" -f 4
0004
PS C:\> "{0:0000}" -f 45
0045
PS C:\> "{0:0000}" -f 456
0456
PS C:\> "{0:0000}" -f 4567
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 Foreach 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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

5 thoughts on “Format Leading Zeros in PowerShell”

  1. Conrad says:
    January 3, 2012 at 11:59 am

    Wouldn’t a
    “{0:D4}”
    Also do the same thing?

  2. JV says:
    January 3, 2012 at 12:08 pm

    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.

    1. Jeffery Hicks says:
      January 3, 2012 at 12:12 pm

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

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

  3. JV says:
    January 3, 2012 at 12:25 pm

    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.

    1. Jeffery Hicks says:
      January 3, 2012 at 12:27 pm

      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.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d