Introduction

There are many methods to format strings in PowerShell. Some are easy, some are very powerful. In this article explained some methods that you can use for this task.

Concatenation of strings

Easiest method that allows you to clue together one or more strings, or other types of variables.
PS C:\> $Result = 13 PS C:\> "Result is " + $Result + " seconds." Result is 13 seconds.
Only one thing to note: first value should be string, or you will get an error.
PS C:\> $Result = 13 PS C:> $Result + " seconds total" Cannot convert value "seconds total" to type "System.Int32". Error: "Input string was not in a correct format."

This occurs because PowerShell tries to convert the right operand (or operands) into the type of specified by the left operand. In this case it tries to convert "seconds total" to integer and, of course, fais. To workaround this you can use an empty string as first value ("" + $Result + " seconds total") or use method that follows.

Some cmdlets also concatenate strings for you. For example Write-* cmdlets join all unnamed arguments passed to it into single string.

PS C:\> write-host $Result "seconds total." 13 seconds total.

Inserting variable values to strings

PowerShell allows you to insert variables directly into strings without concatenations of any kind. Variables will be replaced by their values. This operation can be done only with the strings enclosed in double quotes. Strings in single quotes does not expand any variables in them and present text as is.
PS C:\> $Result = 13 PS C:\> "Result is $Result seconds" Result is 13 seconds PS C:\> 'Result is $Result seconds' Result is $Result seconds
If variable type is not string it will be converted to string using standard .ToString() method which is available in every object. This will not always give expected results:
PS C:\> $Service = Get-Service -Name Spooler PS C:\> "Service is $Service" Service is System.ServiceProcess.ServiceController
Also you can insert a result of some subscript, or reference properties of object to the string. But this requires more complicated syntax. Such subexpressions in the strings should be enclosed in following construction: $() .
PS C:\> "Service is $($Service.Name)" Service is Spooler PS C:\> "Result is $(3 + 10)." Result is 13.
If you want some variables to expand into values and some to remain inact, you can do that by masking$ symbol using ` (Grave accent or backtick) symbol before it.
PS C:\> "`$Result is $Result" $Result is 13

Array separator

When you insert variable with an array of values into string PowerShell not only convert all array elements to strings, it also joins them into one string. By default it uses space as separator when joining this elements together.
PS C:\> "Numbers is $Array" Numbers is 1 2 3 4 5
You can specify any string to use as a separator instead of single space. Just assign this string to $OFS (Output Field Separator) variable.
PS C:\> "Numbers is $Array" Numbers is 1, 2, 3, 4, 5
Also it is possible to use -Join operator to get similiar effect.
PS C:\> "Numbers is $($Array -join ', ')" Numbers is 1, 2, 3, 4, 5

.NET formatting

Also PowerShell can use .NET string formatting. Usually it is done with -f operator. You can specify some positions in string using special syntax (index in curled braces), insert after this string -f operator, and after it an array which elements will be pasted to designated positions:
PS C:\>$TimeElapsed = 15 PS C:\>$Result = 13 PS C:\>"Script finished in {0} minutes and result is {1}." -f $TimeElapsed, $Result Script finished in 15 minutes and result is 13.
But this method is not limited to pasting values into designated places. It also possible to align text to left or right by padding with spaces:
PS C:\>"{0,6} lines" -f "Abc" Abc lines PS C:\>"{0,-6} lines" -f "Abc" Abc lines
Much more interesting stuff is possible with integers. You can pad them with zeros by placing a colon after index identifier and then some amount of zeros.
PS C:\>"{0:00000}" -f 181 00181
There are some special number specifiers for different number formatting, represented by letters. After that letter you can optionally specify number zeros (which meaning is different depending from the number specifier).

Letter C represents currency. Note that currency symbol, decimal separator and negative amount formatting depends from system regional settings:

#Russian regional settings: PS C:\>"{0:C2}" -f 181 181,00?. PS C:\>"{0:C2}" -f -181 -181,00?. #English - United States: PS C:\>"{0:C2}" -f 181 $181.00 PS C:\>"{0:C2}" -f -181 ($181.00)
F letter allows to set how many decimal symbols to show, and also region dependent:
#Russian PS C:\>"{0:F2}" -f 1.8292 1,83 #English - United States PS C:\>"{0:F2}" -f 1.8292 1.83
Letter X stands for heX and provides a simple way to convert number to a hexadecimal representation, and to pad some zeros if you need specific length of string:
PS C:\>"{0:x}" -f 8102 1fa6 PS C:\>"{0:X}" -f 8102 1FA6 PS C:\>"{0:X6}" -f 8102 001FA6
You can find full documentation of this numeric format specifiers on MSDN, and see more examples here. Examples are in C# but its easy to convert them into PowerShell if you know the basics.

See Also

Other Languages