Microsft PowerShell scripting is little different from the other Programming languages. In most of the programming languages to concatenate a string you will use the operator “+” but in PowerShell, this will not work. So How to Concatenate Strings and Variables in PowerShell.
Introduction to PowerShell Concatenate String
Let’s take a simple example of PowerShell Concatenate String
$employee = New-Object psobject -Property @{
Id = 42
FirstName = "Mark"
LastName = "Zuckerberg"
}
If you look at the above example I have a PowerShell object of type employee with Properties ID, FirstName, and LastName. Let's try to concatenate properties FirstName and LastName using "+" operator and see what will be the output result.
$employee = New-Object psobject -Property @{
Id = 42
FirstName = "Mark"
LastName = "Zuckerberg"
}
Write-Host $($employee.FirstName) + ($employee.LastName)
# Output
# Mark + Zuckerberg
The “+” operator doesn’t work in the PowerShell for concatenating the Strings and Variables.
$a = "Hello"
$b = "World"
$a + $b
# Output
# HelloWorld
In the above example when we tried to concatenate two variables using the + operator it did work and just glued both the strings as HelloWorld. But it doesn’t work in all the case as we see with the objects and properties it just failed.
How to Concatenate Strings and Variables in PowerShell
There are multiple ways to Concatenate Strings in PowerShell. Let’s look at the most efficient way to perform the PowerShell concatenation.
#1. Expanding Strings
The first approach is very simple and most efficient way to concatenate variables in PowerShell. Use the double quotes instead of the concatenation operator and it works in both the cases. You could easily concatenate anything efficiently.
$employee = New-Object psobject -Property @{
Id = 42
FirstName = "Mark"
LastName = "Zuckerberg"
}
Write-Host "$($employee.FirstName) $($employee.LastName)"
# Output
# Mark Zuckerberg
$a = "Hello"
$b = "World"
"$a $b"
# Output
# Hello World
If you would like to add any characters while appending we could do that easily. Let’s get we need a comma after the first word during concatenation. In order to do that, you just need to add the character after the first variable. Here is the example.,
$a = "Hello"
$b = "World"
"$a, $b"
# Output
# Hello, World
#2. Substitution or string.Format approach
The expanding strings might be often difficult to read if you have a lengthier code of concatenation. In order to simplify, you can use the string and parameter based approach as shown below.
$employee = New-Object psobject -Property @{
Id = 42
FirstName = "Mark"
LastName = "Zuckerberg"
}
Write-Host ("{0} {1}" -f $employee.FirstName,$employee.LastName)
# Output
# Mark Zuckerberg
If you are a c# programmer then you will be very familiar with the above approach. This is similar to string.format approach which we use in C# to concatenate the strings.
#3 Join Operator
The other way is to use Join operator, below are few ways on how you could use Join to conatenate two strings.
$s1 = "Hello"
$s2 = "Its a beautiful world"
-join ($s1, $s2)
$s1, $s2 -join ", "
# Output
# HelloIts a beautiful world
# Hello, Its a beautiful world