PowerShell Join Operator
In this tutorial we will go through PowerShell Join Operator. -Join
Operator combines multiple strings into one. Strings are combined in the order that they are appear. The format of the command is as below:
-Join <String[]> <String[]> -Join Delimiter
Following we will see few examples of what -join
operator can do and you will understand better on how this can help you out in your scripts.
If you provide strings that are comma separated, only the first string is will be submitted to -join
and we will have the result below.
Code:
-join "Hello" , "World.", "This" , "is" , "a" , "beautiful" , "day."
Result:
In order to submit all strings to -join operator you need to enclose them in parentheses or or store them in variables. We will use the same strings to see the difference with the above.
Code:
-join ("Hello" , "World.", "This" , "is" , "a" , "beautiful" , "day.")
Result:
Code:
$a = "Hello" , "World.", "This" , "is" , "a" , "beautiful" , "day." -join $a
Result:
[adinserter name=”In Article”]
Delimiter
For our example above in order for the sentences to be readable, we have to use space between each of our strings. We have to define a value to the delimiter. The default delimiter is ""
. So when we used -join to combine strings there is no space between them. We will use " "
as a delimiter for our next example. It does not matter how you will submit the strings in -join
, when there is a use of delimiter. -Join
will handle them the same. Below are the commands used with the delimiter.
Code:
"Hello" , "World.", "This" , "is" , "a" , "beautiful" , "day." -join " " ("Hello" , "World.", "This" , "is" , "a" , "beautiful" , "day.") -join " " $a = "Hello" , "World.", "This" , "is" , "a" , "beautiful" , "day." $a -join " "
Results:
As a delimiter you can use multiple characters to join strings. In the following example we are using "oot"
as delimiter to join the strings and we receive the below result.
Code:
$a = "I broke my f" , " and I am not able to wear my b" , "s for hiking." $a -join "oot"
Result:
[adinserter name=”In Article”]
Here-Strings
-join
operator can be used in here-strings also. Here-string is considered a single string, so -join
operator will join both here strings the same way. In the below examples we use -join
to combine the two here-strings. In the first example we do not use any delimiter and as you will see -join
operator will combine the two strings without any space at all. The example example uses a special character for new line, "`n"
, as a delimiter in order to combine the two here-string by adding a new line for each new here-string added to the first one.
Code:
$a = @" Hello World I love you "@ $b = @" Hello under World I have you "@ -join ($a , $b)
Result:
Code:
$a = @" Hello World I love you "@ $b = @" Hello under World I have you "@ $a , $b -join "`n"
Result:
For here-strings we are also able to use join to change the string from a multi line to single line. As we said before, a here-string is only a single string, so before we will be able to use -join , we will have to split the here-string, in multiple ones by using -split. In the below example we split the here string and then we comine it again, using space ( ” ” ) as a delimiter.
Code:
$a = @" Hello World. This is a beutiful day. The sun is shining. "@ (-split $a) -join " "
Results:
I hope the tutorial about PowerShell Join Operator is helpful.
Please let me know your comments and thoughts.
You feedback is appreciated.
[adinserter name=”In Article”]
Related Links:
[adinserter name=”Matched-Content”]


Leave a Reply