PowerShell Concatenating strings


Using variables in a string

You can concatenate strings using variables inside a double-quoted string. This does not work with properties.

$string1 = "Power"
$string2 = "Shell"
"Greetings from $string1$string2"

Using the + operator

You can also join strings using the + operator.

$string1 = "Greetings from"
$string2 = "PowerShell"
$string1 + " " + $string2

This also works with properties of objects.

"The title of this console is '" + $host.Name + "'"

Using subexpressions

The output/result of a subexpressions $() can be used in a string. This is useful when accessing propeties of an object or performing a complex expression. Subexpressions can contain multiple statements separated by semicolon ;

"Tomorrow is $((Get-Date).AddDays(1).DayOfWeek)"