Active Directory Powershell: Export all users with a specific email domain

Bühler Gabriel 81 Reputation points
2023-08-07T14:23:42.49+00:00

Hello Everyone

I am trying to export a List of all Users who have the same E-Mail-domain (for Example *@email.com) so I set up this script:


$results = Get-ADUser -Filter * -SearchBase "OU=OUNAME,OU=OUABOVE,OU=OUAbove, OU=MAINOU,DC=DOMAINNAME,DC=net"  -Properties "SAMAccountname", "GivenName", "Surname", "Title", "Division", "Department", "EmailAddress" , "Manager", "TelephoneNumber" | Select "SAMAccountname", "GivenName", "Surname", "Title", "Division", "Department", "EmailAddress" , "Manager", "TelephoneNumber"
$filter= $results | where {$_.email -contains "*@email.com"}
$export = $results | Export-CSV "C:\Users\GBU101\external_user_superiormail.csv" -Append -NoTypeInformation


I also tried with "Emailaddress" and "Email" but it was not able to find anything.

Do you may know what the issue could be?

Thank you for your help.

Kind regards,

Gabe

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,932 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,906 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,856 Reputation points
    2023-08-07T15:20:32.2466667+00:00

    It might be the extraneous space in the Organizational Unit name:

    "OU=OUNAME,OU=OUABOVE,OU=OUAbove, OU=MAINOU,DC=DOMAINNAME,DC=net"
                                     ^
                                     |
                                     +---- This
    

    But it's also (more) likely that it's use of "-contains" instead of "-like" in the Where-Object cmdlet, and your use of the property "mail" instead of the "EmailAddress" in the same conditional statement.

    You can also make your code more readable:

    $props = "SAMAccountname", "GivenName", "Surname", "Title", "Division", "Department", "EmailAddress" , "Manager", "TelephoneNumber" 
    Get-ADUser -Filter * -SearchBase "OU=OUNAME,OU=OUABOVE,OU=OUAbove,OU=MAINOU,DC=DOMAINNAME,DC=net"  -Properties $props |
        Select-Object $props | 
            Where-Object {$_.EmailAddress -LIKE "*@email.com"} |
                Export-CSV "C:\Users\GBU101\external_user_superiormail.csv" -Append -NoTypeInformation
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Ian Xue-MSFT 41,691 Reputation points Microsoft External Staff
    2023-08-09T05:52:23.29+00:00

    Hi

    The "-contains" operator tells whether a set includes a certain element. In your case, the only element of the emailaddress property is the mail address itself. You can use a matching operator such as "-like" or "-match" to match a string to a specified pattern.

    $results | where {$_.EmailAddress -match '@lab.local'}

    $results | where {$_.EmailAddress -like '*@lab.local'}

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


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.