Updated March 6, 2023
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
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:
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.
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:
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:
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:
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
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:
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:
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:
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:
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:
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:
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:
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:
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 –