I want to get email address of all the users that are indirectly in one OU, in that OU there are groups and the groups members are sometimes groups and sometimes users (I need all the users in all groups and subgroups) so I write the following script
$groups = Get-ADGroup -Filter * -SearchBase 'OU=someOU,DC=someDomain,DC=com'
foreach ($group in $groups)
{
$groupDN = (Get-ADGroup $group).distinguishedName
Get-ADuser -LDAPFilter "(memberOf:1.2.840.113556.1.4.1941:=$groupDN)" |ft
}
and the problem is that some users are member of different groups and it shows them more than once . I also tried other way
Get-ADobject -LDAPFilter "(&(objectClass=person)(objectClass=user)(memberof:1.2.840.113556.1.4.1941:='OU=someOU,DC=someDomain,DC=com'))"
and it does not show anything, also tried this
Get-ADobject -Filter * -SearchBase 'OU=someOU,DC=someDomain,DC=com' -SearchScope Subtree | Select-Object name
which shows only the qroups in that OU but I want group members and nested group members. can someone solve my problem thanks in advance

You could store all users in the array and then filter out duplicates