Formatting numbers as string in PowerShell

I had to look this up again today (easily the 5th time in the last month). So, to make it easier to look up the 6th time, I'm putting it here.

If you want to format a number as a string, the quickest way is with the –f operator:

$serverName = "ABC{0:000}D" –f $srvID

The above line, with the $srvID variable equal to 13 will set the $serverName variable to "ABC013D"

More information: http://social.technet.microsoft.com/wiki/contents/articles/4250.string-formatting-in-powershell.aspx#NET_formatting

comments powered by Disqus