ridicurious.com
Powershell, PSTip

Adding leading zero with PowerShell Format Operator

I often find myself in situations when I want to PSRemote multiple servers with server names in such a series that has same prefix server names and suffixed by a 2 digit number like-

sameprefix05
sameprefix06
sameprefix07
sameprefix08
sameprefix09
sameprefix10
sameprefix11
sameprefix12
sameprefix13
sameprefix14
sameprefix15

and to avoid saving the server names in a file and directly PSRemote them, I do something like in the below screenshot to generate the series
PowerShell Format Operator

This is smart, but that doesn’t work because the single digit numbers don’t comply the server name nomenclature in the above example, which should be like 05 instead of 5

So I was wondering, there has to be a better way.

To our rescue PowerShell provides -f format operator ( PowerShell Format Operator ) which could be used to make this work.

This small trick helps me a lot in day-to-day operations and hopefully, will help you as well. You can find more such PowerShell Tips here.

Please do follow me on twitter for more Interesting PowerShell material and don’t forget to Show-off cool trick to your colleagues, thanks for reading. Cheers! 😉

Subscribe to our mailing list

* indicates required

Related posts

Powershell : Using Online Dictionaries to Data Mine Word Meanings in one shot

Prateek Singh
9 years ago

PowerShell to C# and Back – Introduction to Classes

Prateek Singh
4 years ago

Powershell Auto Mute, when headphones are accidentally unplugged

Prateek Singh
7 years ago
Exit mobile version