Run operations concurrently:
$vms = Get-AzVM -ResourceGroupName "myRG"
$vms | ForEach-Object -Parallel {
$vm = $_
Write-Output "Processing $($vm.Name)"
Stop-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Force
} -ThrottleLimit 5The -ThrottleLimit controls max concurrent threads.
Note: Variables from the outer scope need $using: prefix:
$tag = "environment"
$vms | ForEach-Object -Parallel {
$t = $using:tag
Get-AzTag -ResourceId $_.Id | Where-Object { $_.Properties.TagsProperty.ContainsKey($t) }
} -ThrottleLimit 10