Powershell

How to Concatenate Strings in PowerShell

Different operations can be performed using strings in programming fundamentals, such as comparing strings, searching a character in a string, and concatenating strings. Sometimes, lines have a longer length, and the length factor affects the understandability of the code. So, to avoid such occurrences, a larger string is broken down into smaller strings for better readability of the source code. Finally, those smaller strings are joined together, and this phenomenon is known as concatenation. The most common practice to concatenate strings is by using the “+” operator. However, various string methods are also used to perform concatenation, such as “String.Concat“, “String.Join“, and “String.Format“. We will try to cover all possible ways to concatenate a string. In this article, a detailed guide is provided to concatenate strings using PowerShell ISE:

What Are the Possible Ways to Concatenate Strings in PowerShell:

This section enlists possible ways to join multiple strings together. Each option is explained with an example.

First, you have to access the “PowerShell ISE” with administrator privileges. To do that, click on “Search” on the taskbar and find “PowerShell ISE“. Once you got the search result, right-click on the “PowerShell ISE” app, and click on “Run as administrator“.

What Are the Operators Used to Concatenate Strings, and How to Use Them?

There are multiple operators to concatenate strings. We will shed light on those operators and explain them with examples.

How to Concatenate Strings Using “+”:

The most common and frequently used method to concatenate strings is using the “+” operator.

We will take three strings and join them using the “+” operator; the code to concatenate using three strings is given below. Copy and paste the code in the Script pane of your PowerShell ISE and run the script to check the output. However, the number of strings varies according to requirement. For instance, we have concatenated three strings, and one can join two or four strings:

$s1= “Hi, ”
$s2= “this is PowerShell ISE and; ”
$s3= “you are concatenating strings:”
$s4=$s1+$s2+$s3
$s4

The script is saved as “con1.ps1“:

The output of the code is given below:

If you want to create space in the output, you must follow the following syntax using the “+” operator. The code to perform such action is given below:

$st1= "this is 1st string"
$st2= "and this is 2nd string"
$res=$st1+ “ ” +$st2
$res

We have created script “con2.ps1“:

The output is given below:

How to Concatenate Strings Using Format(-f) Operator:

It is not necessary to stick to one method for concatenation. Different operators are used for concatenation, and the operator “-f” works as demonstrated in the code below:

$s1= “hello!
$s2= “string concatenation ”
$s3= “in progress”
$res= “{0}{1}{2}-f $s1,$s2,$s3
$res

The number written in curly braces shows the index of strings. The first string to be considered is at “index 0“, the second string at “index 1“, etc. We have saved the code in script “con3.ps1“:

The output of the above code is given below:

What Methods Are Used to Concatenate Strings in PowerShell:

There are different PowerShell methods available to concatenate strings. We will discuss those methods and will demonstrate the way to use those methods:

1. Concat Method

The Concat method of strings is also used to join multiple strings. The following code will concatenate four strings. Moreover, there is no limit on the number of strings selected for concatenation:

$s1= “first string::”
$s2= “second string::”
$s3= “third string::”
$s4= “fourth string.”
$res=[string]::Concat($s1, $s2, $s3, $s4)
$res

The script of the code is given below, “con4.ps1“:

The output of “con4.ps1” is shown below:

2. Join Method

Apart from the “Concat” method, another PowerShell method called “Join” concatenate strings. The “Join” method concatenate strings using a delimiter as given below. The colon will be placed between strings in the output. However, you can use any string or character in the “Join” method as a delimiter. The following code shows the use of the “Join” operator:

$s1= “1st string ”
$s2= "2nd string"
$s3= "3rd string"
$s4= "4th string."
$res=[string]::Join(“:”, $s1, $s2, $s3, $s4)
$res

We have created script “con5.ps1“, and the delimiter position is highlighted as “delimiter“. Anything written in this position will be considered a delimiter.

The output of the script “con5.ps1” is shown below:

3. Format Method

The format method can also be used to concatenate strings. The functionality of format method is the same as format operator. The code below shows the operating procedure of the format method:

$s1= “Hello!
$s2= “this is ”
$s3= “Format method.”
$res=[string]::Format("{0}{1}{2}", $s1, $s2, $s3)
$res

Three strings are used to concatenate them using the format method. The script of this method is given here:

The output of the above script is shown below:

Conclusion:

The length of strings varies according to the program’s requirement. At some point, you have to encounter some larger strings, which makes the strings too complicated to understand. The primary reason for concatenation is to join multiple strings.

This guide briefly discusses various ways to concatenate strings, including some “PowerShell methods” and few “operators“. Although all the methods and operators work well, built-in PowerShell methods are highly recommended for concatenation because the operators are generic and are used as in other programming languages.

About the author

Adnan Shabbir