Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

PowerShell slice array into groups of smaller arrays

This is a small script to slice array into groups of smaller array, and I use it in my scripts most often. This script is very helpful when I want to batch process multiple server at time but not on all the server, instead fewer servers at servers in batch. Once I have a very big fat list of servers, create chunk out of it for processing. I am using for loop with increment of given number to cut array in to slices and joining the result with , comma.

Microsoft powershell slice array into group of array cut group array into smaller array foreach-object for if else elseif join.png

In the next example I using same technique to get first and last number for other usage.

Microsoft powershell slice array into group of array cut group array into smaller array foreach-object for if else elseif join.png groupof group-object.png

I can add two .. dots to create a range and use something like below.

$test =  {5..10}
(1..100)[(& $test)]

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$numbers = 1..100
$groupOf = 20

for ($i = 0; $i -le 100; $i+= $groupOf)
{
    if ($numbers.count -eq $i)
    {
        break
    }        
    $group = $i + $groupOf
    $numbers[$i..$group] -join ', ' 
    $i++
}


$numbers = 1..100
$groupOf = 9

for ($i = 0; $i -le 100; $i+= $groupOf)
{
    if ($numbers.count -eq $i)
    {
        break
    }
    $group = $i + $groupOf   
    "$i .. $group"
    $i++
}

Further more if you need a carved out array into variable for later use or need to export to somewhere else you can use .net object System.Collections.ArrayList to collect information so It can be uses however you want.
Microsoft powershell slice array into group of array cut group array into smaller array foreach-object for if else elseif join system.collections.arraylist new-object add method.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
$numbers = 1..100
$groupOf = 9

$result = New-Object System.Collections.ArrayList

for ($i = 0; $i -le 100; $i+= $groupOf)
{
    if ($numbers.count -eq $i)
    {
        break
    }
    $group = $i + $groupOf   
    [void]$result.add($numbers[$i..$group])
    $i++
}

$result[3]

Download Get-SlicedArray.ps1 here or it is also available on github.com/kunaludapi.

Useful Articles
Powershell Convert range of numbers into another list of numbers maintaining ratio
Powershell Active Directory: List complete hierarchy of upstream nested groups recursively of User
Powershell Active Directory: Show treeview of User or Group memberof hierarchy
Powershell Active Directory: Show treeview of nested Group members downstream hierarchy
Oneliner Microsoft Powershell Script Get members from a list of group from Active Directory in excel
Powershell Active Directory 1: Check, enable and disable child OU protect object from accidental deletion
Oneliner Powershell how to add or remove AD user members and objects in groups

Go Back

Comment

Blog Search

Page Views

4142832

Follow me on Blogarama