Loop through Domains in Forest RRS feed

  • Question

  • Trying to export all users in the Forest that have an email address populated in their AD account. The results are a little smaller than I expected, so not sure if I have this loop correct. There is a top-level domain and 4 child domains in the Forest.

    I added the -Server parameter based on other searches that suggested you need to add a Global Catalog. It does seem to increase the search results, but I'm still about 200 mailboxes shy.

    Import-Module ActiveDirectory
    foreach ($Domain in $Domains)
    {Get-ADUser -Filter {EmailAddress -like "*"} -Properties * -Server | select-object DisplayName,EmailAddress,GivenName,Surname,mailNickname,SamAccountName | Export-Csv C:\AllUsers.csv}

    Thursday, November 16, 2017 3:52 PM


  • You can get all users in the forest by just querying the GC.

    Get-ADUser -Filter {EmailAddress -like "*"} -Properties * -Server  | 
         select-object DisplayName,EmailAddress,GivenName,Surname,mailNickname,SamAccountName |
         Export-Csv C:\AllUsers.csv

    There is no need to query every domain.  The GC contains all domains.


    • Marked as answer by Vegas577 Thursday, November 16, 2017 6:59 PM
    Thursday, November 16, 2017 6:39 PM