0

I have following code which will write any users which are part of the group or not:

$Host.UI.RawUI.WindowTitle = "User Group Finder"
$groupname = Read-Host -Prompt 'Enter group name: '

Write-Host ""
Write-Host "People who are not in this group:" -ForegroundColor Red

$results = @()
$users = Get-ADUser  -Properties memberof -Filter * 
foreach ($user in $users) {
    $groups = $user.memberof -join ';'
    $results += New-Object psObject -Property @{'User'=$user.name;'Groups'= $groups}
    }

$results | Where-Object { $_.groups -notmatch $groupname } | Select-Object user

Write-Host "People who are in this group:" -ForegroundColor Green

$results | Where-Object { $_.groups -match $groupname } | Select-Object user

The code is working when i wanna have only one output from it.

But i want to have two different outputs (People who are in group and people who are not).

Problem is for now its combining outputs.

Is there any way i could generate two different output from one psObject?

Johny Wave
  • 111
  • 2
  • 11
  • But you already have two different outputs based on `-match` and `-notmatch` from one psObject: `$results` – Jawad Jan 21 '20 at 19:17
  • @Jawad I know. But when you run the script you will get only one combined output. Like this: https://imgur.com/a/Vda0x9j – Johny Wave Jan 21 '20 at 19:20

1 Answers1

2

Your group results are correctly separated.

The problem is that you are using both the console through Write-Host and the pipeline (output of both results).

Both are displayed on the console, but what goes through the pipeline is not synchronous with Write-Host

That's why things appears out of order.

Send the outpout to the host to fix by piping the results to Out-Host to preserve order when mixed with Write-Host statements.

$results | Where-Object { $_.groups -notmatch $groupname } | Select-Object user | Out-Host
Write-Host "People who are in this group:" -ForegroundColor Green
$results | Where-Object { $_.groups -match $groupname } | Select-Object user | Out-Host

| Out-String | Write-Host can also be used if you want to specify a different color for the output.

Sage Pourpre
  • 9,932
  • 3
  • 27
  • 39
  • Thank you, had no idea, still learning powershell. Learned something new :-) Marked as correct answer. – Johny Wave Jan 21 '20 at 19:24
  • Note that you can replace `Out-String | Write-Host` with `Out-Host`. The lack of synchronization between host and pipeline output is limited to PS v5+ and a very specific - albeit still common - scenario: implicitly _table_-formatted output for types that do _not_ have formatting data defined for them - see [this answer](https://stackoverflow.com/a/43691123/45375). – mklement0 Jan 21 '20 at 20:21
  • Only just saw your comment now (you didn't @-mention me) - thanks for updating. Yes, `Out-Host` is not widely known, but it's handy for sending richly formatted object representations to the host. Note that, aside from formatting, another important difference between `Write-Host` and `Out-Host` is that in PSv5+ only `Write-Host` writes to the information stream, whereas `Out-Host` truly writes directly to the host, which means that its output cannot be captured. – mklement0 Jan 22 '20 at 13:16