Powershell Tip: Adding leading zeros


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
1

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 which could be used to make this work.

1

1.png

This small trick helps me a lot in day-to-day operations and hopefully, will help you as well.

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! 😉

signature

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s