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“:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/1-7.jpg" data-lazy- height="127" src="data:image/svg xml,” width=”537″>

The output of the code is given below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/2-7.jpg" data-lazy- height="56" src="data:image/svg xml,” width=”611″>

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“:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/3-7.jpg" data-lazy- height="122" src="data:image/svg xml,” width=”476″>

The output is given below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/4-6.jpg" data-lazy- height="74" src="data:image/svg xml,” width=”483″>

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“:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/5-6.jpg" data-lazy- height="125" src="data:image/svg xml,” width=”511″>

The output of the above code is given below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/6-6.jpg" data-lazy- height="81" src="data:image/svg xml,” width=”475″>

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“:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/7-6.jpg" data-lazy- height="153" src="data:image/svg xml,” width=”478″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/8-5.jpg" data-lazy- height="70" src="data:image/svg xml,” width=”566″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/9-6.jpg" data-lazy- height="167" src="data:image/svg xml,” width=”651″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/10-7.jpg" data-lazy- height="75" src="data:image/svg xml,” width=”509″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/11-6.jpg" data-lazy- height="152" src="data:image/svg xml,” width=”640″>

The output of the above script is shown below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/12-4.jpg61132d30678f6.jpg" data-lazy- height="71" src="data:image/svg xml,” width=”563″>

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.