0

I am working on a PowerShell script with a menu to select different ways to search info in Active Directory. The command with Get-ADUser works correctly, but the command with Get-ADPrincipalGroupMembership has an odd behavior. When I select that option (3) and enter a username and run the command nothing gets output to the display, if I then quit by using (Q) on the menu the groups then display.

function Show-Menu { param ( [string]$Title = 'AD Search Tool' ) cls Write-Host "================ $Title ================" Write-Host "1: Search Users by Title" Write-Host "2: Press '2' for this option." Write-Host "3: List User's Groups" Write-Host "Q: Press 'Q' to quit." } do { Show-Menu $input = Read-Host "Please make a selection" switch ($input) { '1' { cls $title = Read-Host -Prompt 'Enter Title' if ($title){ Get-ADUser -Properties SamAccountName, Enabled, Title, EmployeeID -Filter "(Title -eq '$title') -and (Enabled -eq 'True')" | select Enabled, EmployeeID, Name, SamAccountName, Title } } '2' { cls 'You chose option #2' } '3' { cls $user = Read-Host -Prompt 'Enter User Name' if ($user){ Get-ADPrincipalGroupMembership $user | select name | sort name } } 'q' { cls return } } pause } until ($input -eq 'q') 

1 Answer 1

1

Just use Write-Host to write the result immediately. To do it through the pipe, you can use Out-String first.

Therefore

 Get-ADPrincipalGroupMembership $user | select name | sort name | Out-String | Write-Host -ForegroundColor Cyan 

instead of

 Get-ADPrincipalGroupMembership $user | select name | sort name 

will do the trick. * Color is optional. I just like cyan.

Sign up to request clarification or add additional context in comments.

1 Comment

Worked perfectly, thanks so much. Cyan is an ok color I guess but I'll pass. lol

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.