. Advertisement .
..3..
. Advertisement .
..4..
PowerShell is, without any doubt, one of the most popular programs that Microsoft has put out. However, it’s also among the hardest when we consider the scripting language. That is why many people have to look online for guides on simple things such as how to concatenate strings in PowerShell.
How To Concatenate Strings in PowerShell
With + Operator
The most well-known and easiest approach to this issue is to use the + operator. However, you must keep in mind that this concatenation method cannot work if even one variable is not of type string.
Another interesting thing is that the + operator will automatically go back to its mathematic nature if it detects two or more integer values.
$concatString = $str1 + $str2 + $str3
Write-Output $concatString
With Separators
Commas can work the same way as the + operator. However, it has a core difference in that you must envelop the strings with double quotation marks. Otherwise, they will be misinterpreted as separate list properties.
As this method turns the string into a literal expression, it can work with many other separators, not just commas.
$concatString = "$str1 , $str2 , $str3"
Write-Output $concatString
With Integers
In normal circumstances, you can only get an invalid type error if you try to concatenate strings using integers. The only way to work through this issue is by taking the variable’s type before. To do so, simply call the variable first when you code the expression.
$int1 = 0
$concatString = $int1 + $str1
#int variable before string variable
Write-Output $concatString
With String Substitution
This approach works the best when you want to concatenate strings that have different data types. All you need to do is to substitute the strings and put them together.
$int1 = 0
$concatString = "$($int1) , $($str2)"
Write-Output $concatString
With –f Operator
The -f
operator takes in string variables and uses them as arguments for pre-built string values. With this nature, it becomes very easy to use it in string concatenation.
$concatString = "{0}.{1}.{2}." -f $str1,$str2,$str3
Write-Output $concatString
With –join Operator
There is practically no difference between –join
and separators in functionality. That is why we can easily use it the same way as we use the separators to concatenate strings.
$concatString = $str1,$str2,$str3 -join "!"
Write-Output $concatString
With the append() function
It’s not an overstatement to say that this approach is the most complex of all string concatenation techniques. You need to call a separate object to initialize this operation, after all.
Then, you need to make use of append()
to concatenate and ToString()
to convert the result back.
$concatString = New-Object -TypeName System.Text.StringBuilder
$null = $concatString.Append($str1)
$null = $concatString.Append($str2)
$null = $concatString.Append($str3)
$concatString = $concatString.ToString()
Write-Output $concatString
With the concat() function
concat()
is a unique function that PowerShell offers in its base library. It is easy to use, as you simply call it, and put the strings in as input arguments.
It can be used for advanced things like exporting CSV.
$concatString = [System.String]::Concat($str1,".",$str2,".",$str3)
Write-Output $concatString
Conclusion
With this guide, you now know a total of eight methods to concatenate strings in PowerShell. You should never underestimate this knowledge, as it can serve as a basis for many advanced techniques later down the line. It’s also something that you will be using all the time, no matter which level you are at.
Leave a comment