简体   繁体   中英

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"

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM