How to Concatenate Strings in PowerShell
Introduction
When working with Strings
, it is common to concate them with some variables to create new strings. In this article, we will show you some methods to concatenate strings.
Solution
Using Plus Operator to Append the Strings
Just like in other language like C#, we can concatenate strings using plus +
operator to append string one after another. But, there is caveat that it must be wrapped in dollar and parentheses symbol $()
to treat it as a single argument.
$year = 2022
Write-Host $("Year " + $year)
# Below script will also work
$year = 2022
$str = "Year " + $year
Write-Host $str
It will result Year 2022
which is correct. But, if we don’t wrap it using dollar and parentheses symbol, it will produce Year + 2022
which is not we expect.
$year = 2022
Write-Host "Year " + $year
Using String Interpolation
String interpolation is conceptually the same with the one in C# where it allows us to insert values to a string literal. To interpolate strings, we need to understand when to use double quote and single quote because it behaves differently.
$year = 2022
Write-Host "Year $year"
Above code will give the below result:
Year 2022
But, if we use single quote using above pattern, it will give unexpected result:
$year = 2022
Write-Host 'Year $year'
The output will be as following which is not we want:
Year $year
To use single quote in strings interpolation, the pattern is to append the string immediately with the variable without any operator in between.
$year = 2022
$month = "December"
Write-Host 'Year'$year', Month '$month
The above script will result below output:
Year 2022, Month December
As an additional note, using single quote like above example will add space between the string and the variable that we want to append.
Using Join Operator
We can also concatenate strings using Join -join
operator.
$year = 2022
$month = "December"
Write-Host (-join("Year ", $year, ", Month ", $month))
Above script will give below output:
Year 2022, Month December
Using PowerShell Built-in String Format Operator
PowerShell has built-in string format operator which behaves similar to C# string format method.
$year = 2022
$month = "December"
Write-Host ("Year {0}, Month {1}" -f $year, $month)
Year 2022, Month December
Using C# or .NET String Format
Since PowerShell supports .NET objects, we can use .NET libraries in PowerShell, thus we can use C# String Format method.
$year = 2022
$month = "December"
Write-Host ([string]::Format("Year {0}, Month {1}", $year, $month))
Year 2022, Month December
Conclusion
In PowerShell, there are many ways to concatenate strings. We can use plus +
operator, string interpolation, join operator as well as string format method both from PowerShell or C#.
However, in general String Interpolation is the most preferable way to concatenate strings since it is more memory-efficient compared to string concatenation using plus +
operator, followed by string format as the second most preferable way to concatenate strings.