2020-06-17T22:32:32+00:00

Here’s a cool way to visualize runspaces and how they work asynchronously.

1
2
3
4
5
6
$IndentLevel = 2
Get-Random; @(1..100) | ForEach-Object -Parallel {
    $i = $_
    Start-Sleep -Milliseconds (Get-Random -Minimum 100 -Maximum 1500)
    "{0,$($i * $using:IndentLevel)}" -f $i | Write-Host
}

The random delay can help show how some tasks finish out of order. For example, running that might result in this in the console:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
       4
 1
     3
   2
             7
         5
                 9
           6
                  10
               8