EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell join array
Secondary Sidebar
PowerShell Tutorial
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell join array

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.

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

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,650 ratings)

$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
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more