• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Stephanos Constantinou Blog

PowerShell Scripting

  • Home
  • Blogs
    • PowerShell Tutorials
    • PowerShell Scripts
    • PowerShell Modules
      • Modules Cmdlets
    • Software Reviews
  • About
  • Contact
You are here: Home / PowerShell Tutorials / PowerShell Join Operator

PowerShell Join Operator

24/05/2018 by Stephanos Leave a Comment

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:

PowerShell Join Operator - Default

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:

PowerShell Join Operator - Parentheses

 

Code:

$a = "Hello" , "World.", "This" , "is" , "a" , "beautiful" , "day."
-join $a

Result:

PowerShell Join Operator - Variable

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

PowerShell Join Operator - Delimiter

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:

PowerShell Join Operator - Character Delimiter

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

PowerShell Join Operator - Here-String - No Delimiter

Code:

$a = @"
Hello World
I love you
"@
$b = @"
Hello under World
I have you
"@
$a , $b -join "`n"

Result:

PowerShell Join Operator - Here-String - Delimiter

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:

PowerShell Join Operator - Single Here-String

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:

  • PowerShell Tutorials
  • PowerShell Scripts
  • PowerShell Split Operator
  • about_Join | Microsoft Docs

[adinserter name=”Matched-Content”]

Summary
PowerShell Join Operator
Article Name
PowerShell Join Operator
Description
PowerShell Join Operator. In this tutorial you will learn how to use -Join operator in PowerShell. Stephanos Constantinou Blog - PowerShell Scripting
Author
Stephanos
Publisher Name
Stephanos Constantinou Blog
Publisher Logo
Stephanos Constantinou Blog

Filed Under: PowerShell Tutorials Tagged With: PowerShell Join, PowerShell Operators

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Footer

Recent Posts

  • ICS Cube Product Review 26/04/2019
  • PowerShell Module SysInfo v1.2.0 15/03/2019
  • PowerShell Module SysInfo v1.1.2 13/11/2018
  • PowerShell Module SysInfo 24/10/2018
  • Get-VoltageProbe 24/10/2018
  • Get-VideoController 24/10/2018
  • Get-USBController 24/10/2018
  • Get-TrackPoint 24/10/2018
  • Get-TrackBall 24/10/2018
  • Get-TouchScreen 24/10/2018
Planet PowerShell

Categories

  • Modules Cmdlets (57)
  • PowerShell Modules (5)
  • PowerShell Scripts (38)
  • PowerShell Tutorials (35)
  • Software Reviews (2)

Archives

  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (1)
  • October 2018 (56)
  • September 2018 (13)
  • August 2018 (9)
  • July 2018 (6)
  • June 2018 (8)
  • May 2018 (7)
  • April 2018 (9)
  • March 2018 (4)
  • February 2018 (6)
  • January 2018 (12)
  • December 2017 (4)
Top 10 PowerShell 2018

Blogroll

  • Planet PowerShell
  • Reddit – PowerShell
  • PowerShell Magazine
  • PowerShell.org
  • PowerShell Team Blog
  • Hey, Scripting Guy! Blog
  • Mike F Robbins
  • PowerShell Explained with Kevin Marquette
  • Mike Kanakos – Network Admin
  • The Lonely Administrator
  • AskME4Tech
PowerShell Blogs Sysadmin Blogs Banners for Top 20 Programming Blogs

© 2023 · Stephanos Constantinou Blog

  • Home
  • Blogs
  • About
  • Contact