EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell join array
 

PowerShell join array

Updated March 6, 2023

PowerShell join array

 

 

Introduction to PowerShell join the array.

PowerShell Join array is the operator to join arrays using the operator and using the methods of the String class. Joining two arrays create the third array because of the fixed-size nature of the array (not the ArrayList). The output of the Join array is also the collection of the Object.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax

The Join Array Operation supports various syntaxes.

  • ‘+’ Operator to Join two string arrays or different datatype array.
  • Join(Char, Object[]): To Concatenate the character to the Object array.
  • Join(Char, String[]): To concatenate the character to the string array.
  • Join(String, Object[]): To concatenate the string to the Object array.
  • Join(Char, String, Int32, Int32): To concatenate the character to the string array from the start index up to the count of the string.

Examples

Here are the following examples mention below

Example #1 – Joining two arrays with ‘+’ operator.

Suppose we have two different arrays, and we need to join them.

$array1 = "Cat","Dog","Cow"
$array2 = "Cars","Bikes","Trucks"
To join two arrays, we will use the ‘+’ operator here.
$array = $array1 + $array2
$array

Output:

PowerShell join array output 1

Example #2 – Joining two arrays with different data types.

In the first example, we have seen the values that two PowerShell arrays stored were the collection of string. However,  we can store different values in the string and Join them.

For example,

$array1 = "Cat","Dog","Cow"
$array2 = 11,"TwoTwo",33,"FourFour"

If we check the data type of the two variables $Array1 and $Array2, it will be a collection of Object[] as shown below.

PowerShell join array output 2

This means the in the array; it doesn’t matter what the different types of objects you enter; ultimately, it becomes the collection of objects. So we can merge any number of arrays using the ‘+’ operator.

In this example, merging two arrays,

$array = $array1 + $array2
$array

Output:

PowerShell join array output 3

In the first two examples, we are using a third array to store the result. Let’s check its datatype; it should be object collection as well.

$array.GetType()

Output:

PowerShell join array output 4

Example #3 – Joining two arrays using ArrayList

In the below example, we will join two arrays using the .Net class System.Collection.ArrayList, and to perform the Join Operation, we need to use the Add() method. As shown below, we have two arrays, and they are joined using the Add() method.

$array1 = "Cat","Dog","Cow"
$array2 = 11,"TwoTwo",33,"FourFour"
foreach($value in $array1){$JoinArray.Add($value) | Out-Null}
foreach($value in $array2){$JoinArray.Add($value) | Out-Null}
$JoinArray

Output:

PowerShell join array output 5

We could directly add the array to the Add method like $JoinArray($Array1), but the problem it treats the entire array as the index 0 and the next array $JoinArray($Array2) as the index 1. So we had to add each value using the foreach loop.

Example #4 – String Join Array Example – I

In the string array, System.String .Net class supports the Join Methods. There are multiple overload methods to Join the array. See the reference link below for the String join method.

https://docs.microsoft.com/en-us/dotnet/api/system.string.join?view=net-5.0#System_String_Join_System_Char_System_Object

PowerShell join array output 6

We will use a few of the methods above.

In the above all Join Methods, the First parameter is always a Separator, and the second is the item to perform the Join operation.

The First String Join method we will use here is, Join(Char, Object[]). Here the Char is the separator, and Object[] is the item to process. It is the array of an Object[], so we can pass any object inside it, and hence it is a string method; it should convert that Object[] to the String array. For example,

$Services = Get-Service
[String]::Join('|',$Services)

Output:

PowerShell join array output 7

As you can see in the above example, we have Joined each services object[] by the separator ‘|’. For this example, the output might be in the proper format, but in some examples like Get-Process, when you pass the entire Object[] as the string. For example,

$procs = Get-Process
[String]::Join('|',$procs)

Output:

PowerShell join array output 8

The above output is not the desired, but instead, we can use its property to separate the Object with the provided separator. For example,

[String]::Join('|',$procs.name)

Output:

PowerShell join array output 9

Example #5 – String Join Array example – II

The second method we can use for the String .Net Class to Join an array is the Join(Char, String[]). It concatenates the string array with the separator mentioned in the first parameter. For example,

$str = "This","is","PowerShell","String"
[String]::Join('^',$str)

Output:

output 10

To Join with Space,

[String]::Join(' ',$str)
This is PowerShell String

Example #6 – String Join Array Example – III

In the above two examples, we have joined a string array using Character. Now we can also concatenate the string using another string because the overload method supports that. In the second parameter, we can pass either Object or a String.

It String concatenation to the array supports two methods.

  • Join(String, Object[]): Concatenates the Object array-like (Get-Service or Get-process) with the string separator between each object output. For example,

$ser = Get-Service
[String]::Join('  Hello  ',$ser)

Output:

output 11

As shown in the above example, services are separated and joined by the Hello keyword, including space.

  • Join(String, String[]): Concatenates the string array using the String separator between each element.

e.g.

$str = "This","is","PowerShell","String"
[String]::Join(' String ',$str)

Output:

output 12

Example #7 – String Join Array Example – IV

In this method, we are using a character or a string as a separator for the Object array[] or the string array[] to concatenate. We can use two separate methods for this.

  • Join(Char, String[], Int32, Int32)

Parameters are described below.

  • Char: A character separator for the Input given
  • String[]: String array to separate and join with a Character.
  • Int32: Starting Index for the concatenation.
  • Int32: Count from the Starting Index.

For example,

$str = "This","is","PowerShell","string","to","test","the","join","array"
[String]::Join('#',$str, 2,5)

Output:

output 13

In the above example, we are using Separator ‘#’ to concatenate string from the starting Index 2 and total count 5 words.

  • Join(String, String[], Int32, Int32)

Similar to the above operation, we can use the Join method to concatenate the string array using another string with the Start Index and the total count.

For example,

$str = "This","is","PowerShell","string","to","test","the","join","array"
[String]::Join(" hello world ",$str, 1,3)

Output:

output 14

Conclusion – PowerShell join an array

PowerShell Join array is a very useful and Powerful operation for the script. It serves many basic purposes like Joining Path, adding value between string array, etc. There are other commands that also serve the Join basics like Join-Path and Join-ADlStoreItem, which indirectly serve the same array operations, but they are advanced commands.

Recommended Articles

This is a guide to PowerShell join the array. Here we discuss the various syntaxes supported by the Join Array Operation along with the examples. You may also have a look at the following articles to learn more –

  1. PowerShell Filter
  2. PowerShell Delete File
  3. PowerShell Registry
  4. PowerShell Continue

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW