How-to: Concatenate in PowerShell

Several methods of combining variables, numbers and strings together are demonstrated below.

In choosing the best method to use, consider the data types you will be working with carefully because PowerShell will treat string data and numbers very differently.

Concatenate strings:

PS C:\> $demo = 'abc' + 'def'
PS C:\> $demo
abcdef

Concatenate string variables:

PS C:\> $string1 = 'abc'
PS C:\> $string2 = 'def'
PS C:\> $string1 + ", " + $string2
abc, def

PS C:\> $text = 'Hello'
PS C:\> $text += 'world'
PS C:\> $text
Helloworld

For longer expressions this can be spread over several lines with line breaks:

PS C:\> $string1 +
 ", " +
 $string2
abc, def

Concatenate numbers with strings:

PS C:\> $int1 = 42
PS C:\> $string2 = 'def'
PS C:\> $int1 + ", " + $string2
Cannot convert value "," to type "System.Int32". Error: "Input string was not in a correct format."

The expression above is evaluated from left to right so converting the string to an integer fails, if we swap around the order they will both convert to a string without error:

PS C:\> $string2 + ", " + $int1
def, 42

String substitution - surround the whole expression in quotes, everything is evaluated as a string:

PS C:\> ”$int1, $string2”
42, def

String Substitution has one important limitation: PowerShell will identify each variable in the expression by matching characters, that are legal for a $variable name, as soon as the first non-legal character is found (a space, comma or full stop) that matching stops.
This means that a subexpression like $object.property will be read as just $object

Explicit sub-expression expansion:

PS C:\> "$($int1), $($string2)"
42, def

Single or Double quoted strings can both be expanded:

$sq = 'a single quoted string'
$dq = "a double quoted string"
PS C:\> "$sq--$dq"
a single quoted string--a double quoted string

Within a single quoted expression no variables are expanded:

PS C:\> '$sq--$dq'
$sq--$dq

Substitution with the -f operator

PS C:\> "{0}--{1}" -f $sq,$dq
a single quoted string--a double quoted string

Join with the -join operator:

PS C:\> $sq,$dq -join ", "
a single quoted string, a double quoted string

A further nuance that often catches people out is that Write-host by default adds a single space separator between items, to remove that you must specify an empty string '' as the separator:

PS C:\> $one = 'one'
PS C:\> $two = 'two'
PS C:\> Write-Host $one $two
one two

PS C:\> Write-Host $one $two -separator ''
onetwo

Using the .Net concat() method:

PS C:\batch> [System.String]::Concat("abc","def", "ghi")
abcdefghi

An alternative concatenation method is to use a StringBuilder .Net object, this has the advantage of being much faster which may be important when working with long strings or repeatedly in a loop.

$stringbuilder = New-Object -TypeName System.Text.StringBuilder
$null = $stringbuilder.Append("Hello")
$null = $stringbuilder.Append("World")
$stringbuilder.ToString()

“I hope someday you’ll join us, and the world will be as one” ~ John Lennon

Related PowerShell Cmdlets

Methods - ToUpper(), PadRight(), Split(), Substring(), Replace() etc.
The -f operator


 
Copyright © 1999-2024 SS64.com
Some rights reserved