Search code examples
powershellactive-directoryfilteringuser-accounts

How do I grab users' email addresses for a specific job Title attribute?


I am trying to run a script to obtain a specific Job title along with their name and email address and export to CSV. The Job title I am looking for is "CW -OTHER". I am not sure what I am doing wrong here. Some steering in the right direction will be appreciated.

Get-ADUser -Filter * -Properties DisplayName, EmailAddress, Title  | select DisplayName, EmailAddress, Title "CW - OTHER" | Export-CSV "C:\Scripts\Email_Addresses.csv"

Solution

  • You are trying to perform that filtering with the Select-Object cmdlet...

    | select DisplayName, EmailAddress, Title "CW - OTHER"
    

    ...which won't work. Use the Where-Object cmdlet for that...

    Get-ADUser -Filter * -Properties DisplayName, EmailAddress, Title `
        | Where-Object { $_.Title -eq 'CW - OTHER' } `
        | select DisplayName, EmailAddress, Title `
        | Export-CSV "C:\Scripts\Email_Addresses.csv"
    

    Note that you could also use the -Filter or -LdapFilter parameters of Get-ADUser to specify a filter to be executed server-side, which would be more efficient. Something like...

    Get-ADUser -Filter { Title -eq 'CW - OTHER' } -Properties DisplayName, EmailAddress, Title `
        | select DisplayName, EmailAddress, Title `
        | Export-CSV "C:\Scripts\Email_Addresses.csv"