0

This is my script it can display all the AD group that is assigned to a user account. I just don't know the syntax on how to do a wildcard search.

Write-Host "'Get AD Groups"

$userName = Read-Host -Prompt "Please enter the LDAP ID"
$ADUser = Get-ADUser -Filter "SamAccountName -eq '$userName'" | Select-Object SamAccountName

if($ADUser -eq $null) {
    Write-Host "$userName does not exist in AD or account is inactive" -ForegroundColor Red
    Continue 
} else {
    $sourceUser = Get-ADUser -Identity $userName -Properties MemberOf
    $sourceGroups = $sourceUser.MemberOf 
    Foreach($group in $sourceGroups) {
        $thisgroup = $group.split(",")[0].split("=")[1]
        Write-Host "$thisgroup"
    }
}
Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84
NOBODY519
  • 1
  • 2
  • I think you're looking for `-Filter "SamAccountName -like '*$userName*'"` or `-Filter "SamAccountName -like '$userName*'"`... the position of the wildcard varies on the need. – Santiago Squarzon Oct 18 '22 at 03:43

0 Answers0