Table of Contents
Using Concatenation Operators
Use concatenation operator represented by +
to concatenate String and variable in PowerShell.
1 2 3 4 5 |
$variable = "together" $concatenatedStr = "Let's learn " + $variable Write-Host $concatenatedStr |
1 2 3 |
Let's learn together |
The +
is the most basic concatenation operator; we used it to concatenate the "Let's learn "
string with the $variable
and stored the new string in the $concatenatedStr
variable, which we further used with Write-Host
cmdlet to display on the PowerShell console.
Don’t forget to enclose the "Let's learn " + $variable
expression within the parentheses (()
) if you are not storing the concatenated string in a variable but joining them while using Write-Host
cmdlet; see the following example.
1 2 3 4 |
$variable = "together" Write-Host ("Let's learn " + $variable) |
1 2 3 |
Let's learn together |
Here is what will happen if you will not enclose it within ()
:
1 2 3 4 |
$variable = "together" Write-Host "Let's learn " + $variable |
1 2 3 |
Let's learn + together |
Use the concatenation operator represented by +=
to concatenate String and variable in PowerShell.
1 2 3 4 5 6 |
$string1 = "together." $string2 = "Let's learn " $string2 += $string1 Write-Host $string2 |
1 2 3 |
Let's learn together. |
The +=
operator is used when we want to perform concatenation and assignment in one step. In the above example, $string1
was concatenated with $string2
, and the new string was assigned to $string2
.
Be careful when using +
and +=
concatenation operators because order matters greatly. See the following example as a demonstration.
1 2 3 4 5 6 7 |
$string1 = "together." $string2 = "Let's learn " Write-Host ($string1 + "Are you going ") $string1 += $string2 Write-Host $string2 |
1 2 3 4 |
together.Are you going Let's learn |
We concatenated the "Are you going "
string with the $string1
variable, which resulted in together.Are you going
as an output. The correct concatenation order was "Are you going " + $string1
. Similarly, we concatenated the $string2
with $string1
using the +=
operator and stored a new string in $string1
, but we printed the $string2
containing the Let's learn
value.
Using String Interpolation with $()
Use string interpolation with $()
to concatenate String and variable in PowerShell.
1 2 3 4 |
$variable = "together" Write-Host "Let's learn $($variable)." |
1 2 3 |
Let's learn together. |
Concatenate string and a variable without using $()
in PowerShell.
1 2 3 4 |
$variable = "together" Write-Host "Let's learn $variable." |
1 2 3 |
Let's learn together. |
This approach is more reader-friendly and easy to comprehend than using concatenation operators.
Using Format Operator
Use the format operator (-f
) to concatenate String and variable in PowerShell.
1 2 3 4 5 6 |
$course1 = "Java" $course2 = "PowerShell" $course3 = "Python" Write-Host ("Let's learn {0}, {1}, and {2}." -f $course1, $course2, $course3) |
1 2 3 |
Let's learn Java, PowerShell, and Python. |
We used the -f
operator in the above example to format the specified string. The format string is "Let's learn {0}, {1}, and {2}."
and $course1
, $course2
, and $course
are the three arguments that we passed in. The format specifiers are the numbers enclosed within the {}
, follow the format of {index:format}
and correspond to the arguments’ indices being passed in. These indices control the order in which the arguments should be displayed on the PowerShell console.
Let’s see another example below.
1 2 3 4 5 6 |
$course1 = "Java" $course2 = "PowerShell" $course3 = "Python" Write-Host ("The average of {0}, {1}, and {2} courses is {3:n2}%." -f $course1, $course2, $course3, 83.5269) |
1 2 3 |
The average of Java, PowerShell, and Python courses is 83.53%. |
In {3:n2}
, the 3
is the index, while n2
means the number of decimal places (the number of digits after the floating-point). You can find more valid format strings here.
Using -join
Operator
Use the -join
operator to concatenate the string with variable(s) in PowerShell.
1 2 3 4 5 6 |
$course1 = "Java" $course2 = "PowerShell" $course3 = "Python" Write-Host ("Available Courses:",$course1, $course2, $course3, "R" -join " ") |
1 2 3 |
Available Courses: Java PowerShell Python R |
The -join
operator joined all the given strings with variables separated by a single white space. Note that you can use any separator that you want.
Using join()
Method
Use join()
method to concatenate string with variable(s) in PowerShell.
1 2 3 4 5 6 |
$course1 = "Java" $course2 = "PowerShell" $course3 = "Python" Write-Host ([string]::join(" ", "Available Courses:",$course1, $course2, $course3, "R")) |
1 2 3 |
Available Courses: Java PowerShell Python R |
Here, we used the join()
method of the string
class; it does the same as the -join
operator, which joins all the strings and variables separated by the specified delimiter. In the join()
method, the first argument is the delimiter; the remaining is the strings and variables that should be concatenated.
Using Concat()
Method
Use Concat()
method to concatenate string with variable(s) in PowerShell.
1 2 3 4 5 6 |
$course1 = "Java" $course2 = "PowerShell" $course3 = "Python" Write-Host ([string]::Concat("Available Courses: ",$course1," ", $course2," ", $course3, " R")) |
1 2 3 |
Available Courses: Java PowerShell Python R |
The Concat()
method also belongs to the string
class and joins all the given strings and variables, but we must be careful about the separator.
Using StringBuilder
Class
Use the StringBuilder
class to concatenate the string with variable(s) in PowerShell.
1 2 3 4 5 6 7 |
$variable = "How are you?" $stringBuilder = New-Object System.Text.StringBuilder $stringBuilder.Append("Hello! ") $stringBuilder.Append($variable) Write-Host ($stringBuilder.ToString()) |
1 2 3 4 5 6 7 |
Hello! How are you? Capacity MaxCapacity Length -------- ----------- ------ 16 2147483647 7 32 2147483647 19 |
Here, we used the New-Object
cmdlet to instantiate the StringBuilder class to access its Append()
method to concatenate the string with a variable. Finally, we used the ToString()
method to convert it into a string and printed it on the PowerShell console using the Write-Host
cmdlet. It also displayed other details about the concatenated string that you can see in the above output.
Using Join-String
Cmdlet
Use the Join-String
cmdlet to concatenate a collection of strings with variable(s) in PowerShell.
1 2 3 4 5 |
$names = "How", "are", "you?" $concatenatedStr = $names + "What's going on?" | Join-String -Separator " " Write-Host $concatenatedStr |
1 2 3 |
How are you? What's going on? |
The Join-String
cmdlet is used to combine or concatenate a string collection into a single string separated by a delimiter specified using the -Separator
parameter. We piped the collection of strings ($names
) to the Join-String
cmdlet. If you want to concatenate a string value with a collection of strings, then you can use the +
operator as we did in the above example.
So, we have multiple ways to concatenate string(s) with variable(s), but choosing the one best suits the project requirements is most important.
That’s all about how to concatenate String and Variable in PowerShell.