Powershell parallel execution

I needed to fetch data about group members from ActiveDirectory (AD) and import it to csv file. While fetching group member information it has become clear that it would take a lot of time. Problem was that some groups had 100K+ members.

Powershell runspaces came to rescue. Function which I used for parallel processing:

function CallScriptInParallel {
Foreach data[i] creates a ps shell and executes script
Waits for completion and returns agg result
param($script, $data, $scriptArgs)
Write-Verbose "Parallel work start"
$RunSpacePool = [runspacefactory]::CreateRunspacePool(1, $numOfCores)
$cEvent = New-Object System.Threading.CountDownEvent(1)
if ($RunSpacePool.RunspacePoolStateInfo.State -ne 'Opened') {
WriteLog "Available Runspaces: $($RunSpacePool.GetAvailableRunspaces())"

$jobs = @()
$ps = @()
$wait = @()

$data | foreach {$i = 0} {
$ps += [powershell]::Create()
$ps[$i].runspacepool = $RunSpacePool
foreach ($arg in $scriptArgs) {

$jobs += $ps[$i].BeginInvoke()
$wait += $jobs[$i].AsyncWaitHandle


$result = @()

$ps | foreach {$i = 0} {
#Skip 1st item as it is a task state
$result += $ps[$i].EndInvoke($jobs[$i]) | select -Skip 1

Write-Verbose "Parallel work done"
return $result