Powershell Tip: Formatting a String with Format

One of the more useful tips that you will use almost everyday within your Powershell development experience will deal with data formatting. Powershell provides you with a Format function that for those of you familiar with data formatting  using String.Format will feel right at home.

The syntax is basically as follows:

[string]::Format(“{format structure}”, data value)

So below I demonstrate a couple of common formatting structures for money, SSNs, and phone numbers but can be expanded upon in an number of ways…

PS C:\> [string]::Format("{0:C}",12.99)
$12.99
PS C:\> [string]::Format("{0:###-##-####}",111223333)
111-22-3333
PS C:\> [string]::Format("{0:(###)###-####}",3178675309)
(317)867-5309

Cheers,

AJ