powershellactive-directorymemberof

Get ADUser sort by memberof not working for me


I've a need to extract a list of all members in a particular OU who are members of the same group. Problem is it only works if I use -match, however that also picks up groups that have a similar name. If I use -eq it retrieves nothing, despite users being members of that particular group. Anyone who might be able to help me figure what I'm doing wrong?

The powershell script I've been using;

Get-ADUser -Filter * -SearchBase 'OU=NewUsers,DC=LOCAL' -properties memberof | Where-Object {$_.memberof -eq 'APP-KMD'} | Select SamAccountName

Solution

  • Apparently you can't match directly, but if you grab the matches on the given group and pull out the members of that group and compare them, it gives the expected output.

    $brugere = Get-ADUser -Filter * -SearchBase 'OU=NewUsers,DC=LOCAL' -Properties * | Select-Object SamAccountName
    $gruppemedlem = Get-ADGroupMember -Identity app-kmd | select SamAccountName
    
    ForEach ($bruger in $brugere)
    {   
    
    If ($gruppemedlem.samaccountname -contains $bruger.SamAccountName) {
        Write-Host $bruger }
    }