How to Split an Array into Smaller Arrays in PowerShell?

Do you need to split a large array into smaller arrays in PowerShell? In this PowerShell tutorial, I will explain different methods to split an array into smaller arrays in PowerShell.

To split an array into smaller arrays in PowerShell, you can create a custom function that utilizes a loop to divide the array into chunks of a specified size. Inside the loop, collect elements up to the chunk size, add the chunk to a collection, and then continue until all elements are processed. Here’s a condensed example of such a function:

function Split-Array {
    param([Array]$Array, [int]$Size)
    $result = @()
    for ($i = 0; $i -lt $Array.Count; $i += $Size) {
        $result += ,@($Array[$i..($i+$Size-1)])
    }
    return $result
}

This function can be called with Split-Array $yourArray 5 to split $yourArray into smaller arrays of 5 elements each.

Split an Array into Smaller Arrays in PowerShell

In PowerShell, you create an array by using the @() syntax and separating items with commas.

For example:

$data = @('Zero', 'One', 'Two', 'Three')

We can split the above type of arrays into smaller arrays in PowerShell using various methods.

Method 1: Using Array Slicing

Array slicing is a method to extract a subset of an array by specifying the start and end indexes.

Here’s a simple example:

$originalArray = @(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
$smallerArray = $originalArray[0..4] # This will create a smaller array containing the first five elements

To split an entire array into smaller arrays of equal size in PowerShell, you could do something like this:

$originalArray = @(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
$batchSize = 3 # Size of each smaller array
$smallerArrays = for ($i = 0; $i -lt $originalArray.Length; $i += $batchSize) {
    $originalArray[$i..($i+$batchSize-1)]+"========="
}
$smallerArrays

You can see the output in the screenshot below once you execute the code using Visual Studio code.

Split an Array into Smaller Arrays in PowerShell

Method 2: Using a Custom Function

You can write a custom function to split the array into smaller arrays. Here’s an example function that does just that:

function Split-Array {
    param(
        [array]$Array,
        [int]$Size
    )
    $parts = [math]::Ceiling($Array.Length / $Size)
    $result = New-Object System.Collections.ArrayList

    for ($i = 0; $i -lt $parts; $i++) {
        $start = $i * $Size
        $end = $start + $Size - 1
        if ($end -ge $Array.Length) {
            $end = $Array.Length - 1
        }
        $result.Add($Array[$start..$end]) | Out-Null
    }

    return $result
}

And you can use this function like so:

$originalArray = @(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
$smallerArrays = Split-Array -Array $originalArray -Size 3

Each element of $smallerArrays will be a smaller array with up to 3 elements.

Method 3: Using .NET Methods

PowerShell can access .NET classes and methods, which can be useful for more advanced array manipulation.

Here’s how you could use the System.Array class to split an array:

$originalArray = @(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
$batchSize = 3
$smallerArrays = New-Object System.Collections.ArrayList

for ($i = 0; $i -lt $originalArray.Length; $i += $batchSize) {
    $elementsLeft = $originalArray.Length - $i
    $sizeOfBatch = [Math]::Min($batchSize, $elementsLeft)
    $tempArray = [System.Array]::CreateInstance($originalArray.GetType().GetElementType(), $sizeOfBatch)
    [System.Array]::Copy($originalArray, $i, $tempArray, 0, $sizeOfBatch)
    $smallerArrays.Add($tempArray +"*******") | Out-Null
}
$smallerArrays

This method creates new array instances and copies elements from the original array into these new smaller arrays.

You can see the output after I executed the PowerShell script using Visual Studio Code.

PowerShell Split an Array into Smaller Arrays

Conclusion

Splitting an array into smaller arrays in PowerShell can be achieved through various methods, such as array slicing, custom functions, or using .NET methods. In this PowerShell method, I will explain how to split an array into smaller arrays in PowerShell using different methods.

You may also like: