1

If I have the below code, I find that Powershell doesnt seem to wait until the Get-ADGroupMember has finished.

$Groups = "Group1", "Group2"

foreach ($Group in $Groups) {
Get-ADGroupMember -Identity $Group -Recursive | Select Name
Write-Host "End of line"
}

From the above I would Expect to get the following (Considering my groups have the same users):

Mike Myers
Tim Sanders
Lucy Gray
End of line
Mike Myers
Tim Sanders
Lucy Gray
End of line

But instead I get:

End of line
Mike Myers
Tim Sanders
Lucy Gray
Mike Myers
Tim Sanders
Lucy Gray
End of line
Bobybobp
  • 95
  • 9
  • 2
    This is a common question. Write-host output seems to come out faster than pipeline output. I can't find a great stackoverflow link about it though. Here's a serverfault one: https://serverfault.com/questions/693549/powershell-write-host-not-synchronous – js2010 Sep 17 '19 at 15:09
  • 1
    `Write-Host` output is written directly to the host console, regular PowerShell output goes to the success output stream. PowerShell does not guarantee output order across streams, only within each stream. – Ansgar Wiechers Sep 17 '19 at 15:16
  • 1
    as js2010 pointed out, the `*-Host` cmdlets write _directly_ to the console host.your other code is writing to the output stream ... and PoSh delays that for ~300 ms to see if there is anything more to group with it. the fix is to **_[A]_** not mix direct & indirect writes to the screen OR **_[B]_** pipe the indirect writes to `Out-Host` to force the item to the screen _right now_. [*grin*] – Lee_Dailey Sep 17 '19 at 15:17
  • Bobybobp, please see the answer to the linked question for a detailed explanation (@Lee_Dailey's comment sums it up). – mklement0 Sep 17 '19 at 15:37

0 Answers0