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')