Small function for splitting an array or hasthtable into smaller chunks.


function New-Chunks {
    [CmdletBinding()]
    param (
        [parameter(ParameterSetName = 'ArrayGroups', Mandatory = $true)]
        [parameter(ParameterSetName = 'ArrayMaxSize', Mandatory = $true)]
        [array]$Array,
        [parameter(ParameterSetName = 'HashtableGroups', Mandatory = $true)]
        [parameter(ParameterSetName = 'HashtableMaxSize', Mandatory = $true)]
        [hashTable]$HashTable,
        [parameter(ParameterSetName = 'ArrayMaxSize', Mandatory = $true)]
        [parameter(ParameterSetName = 'HashtableMaxSize', Mandatory = $true)]
        [int32]$MaxSize,
        [parameter(ParameterSetName = 'HashtableGroups', Mandatory = $true)]
        [parameter(ParameterSetName = 'ArrayGroups', Mandatory = $true)]
        [int32]$Groups
    )
    begin {
        if ($PSCmdlet.ParameterSetName -like 'Array*') {
            $ItemCount = $array.Count
        }
        elseif ($PSCmdlet.ParameterSetName -like 'Hashtable*') {
            
            $ItemCount = $Hashtable.Count
            $Keys = [array]$Hashtable.Keys
        }
        if ($PSCmdlet.ParameterSetName -like '*MaxSize') {
            $Groups = [Math]::Ceiling( $ItemCount / $MaxSize)
            $step = [Math]::Floor( $ItemCount / $Groups)
        }
        elseif ($PSCmdlet.ParameterSetName -like '*Groups') {
            $step = [Math]::Ceiling( $ItemCount / $Groups)
        }
    }
    process {
        for ($i = 0; $i -lt $ItemCount; $i += $step) {
            if ($PSCmdlet.ParameterSetName -like 'Array*') {
                , $array[$i..($i + $step - 1)]
            }
            elseif ($PSCmdlet.ParameterSetName -like 'Hashtable*') {
                $tmpHashTable = @{}
                foreach ($Key in $Keys[$i..($i + $step - 1)]) {
                    $tmpHashTable.Add($Key , $Hashtable[$key])
                }
                $tmpHashTable.Clone()
            }
        }
    }
    end {
    }
}

$res = New-Chunks -Array (1..30) -MaxSize 6
$res[0]