Concatenate String and Variable in PowerShell

PowerShell Concatenate String and variable

Using Concatenation Operators

Use concatenation operator represented by + to concatenate String and variable in PowerShell.

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.

Here is what will happen if you will not enclose it within ():

Use the concatenation operator represented by += to concatenate String and variable in PowerShell.

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.

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.

Concatenate string and a variable without using $() in PowerShell.

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.

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.

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.

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.

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.

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.

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.

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *