Using PowerShell to pull users that are in AD groups from specific OUs

Rench, Brian L 0 Reputation points
2023-06-22T20:09:46.12+00:00

I need to pull a list of users that are in AD groups, but the users are in 2 specific OUs/sub OUs. What can I use in PowerShell to accomplish this?

Windows for business Windows Client for IT Pros Directory services Active Directory
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Tushar Kumar 3,371 Reputation points MVP
    2023-06-22T20:20:31.67+00:00

    Hi Rench, Brian L

    Replace “GroupName” with the name of the group you want to list the users for. Replace “OU1” and “OU2” with the names of the OUs

    Get-ADGroupMember -Identity "GroupName" | Get-ADUser -Properties * | Where-Object {($_.DistinguishedName -like "*OU=OU1,DC=domain,DC=com*" -or $_.DistinguishedName -like "*OU=OU2,DC=domain,DC=com*") -and $_.Enabled -eq $true} | Select-Object Name,SamAccountName
    
    
    

  2. Sukhwinder Singh 51 Reputation points
    2023-06-22T20:21:17.8566667+00:00

    You can use following command to generate list of all users and then in excel just filter on required OU

    get-adgroupmember "groupname" | select name,samaccountname,distinguishedname | out-gridview

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.