PowerShell Script

FCHCAdmin
FCHCAdmin used Ask the Experts™
on
I'm running a script in PowerShell that list all the users first name, last name and e-mail address than exports them to a csv file. I get the columns for first and last name but a blank column in e-mail address, can somebody help? I will really appreciate it. Here's the script:

Import-Module ActiveDirectory
Get-ADUser -SearchBase “OU=people,dc=sanjosefoothillfamilycomm,dc=local” -Filter *  |
 Select givenname, surname, emailaddress |
 Export-Csv "C:\Users\narce\Desktop\script1.csv"
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Because the attribute is named just "mail"
Import-Module ActiveDirectory
 Get-ADUser -SearchBase "OU=people,dc=sanjosefoothillfamilycomm,dc=local" -Filter *  |
  Select givenname, surname, mail |
  Export-Csv "C:\Users\narce\Desktop\script1.csv"

Open in new window

Author

Commented:
I ran the script with only "mail" but get the same result. I'm testing the script so I only created an e-mail address for the first user in the list " "nkozuma" but is not showing in the mail column. Also if I go to AD users and computers under nkozuma in the general tab I can see the e-mail address, attached is the csv file,  any ideas?
script1.csv
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Sorry, forgot the property; mail is no part of the default set:
Import-Module ActiveDirectory
 Get-ADUser -SearchBase “OU=people,dc=sanjosefoothillfamilycomm,dc=local” -Filter * -Property mail |
  Select givenname, surname, emailaddress |
  Export-Csv "C:\Users\narce\Desktop\script1.csv"

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
That was my original script (in the problem description) and is not working. That's the problem
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Sorry again, wrong clipboard; but it's not quite the same, check the arguments for Get-ADUser.
Import-Module ActiveDirectory
 Get-ADUser -SearchBase "OU=people,dc=sanjosefoothillfamilycomm,dc=local" -Filter *  -Property mail |
  Select givenname, surname, mail |
  Export-Csv "C:\Users\narce\Desktop\script1.csv" 

Open in new window

Top Expert 2014
Commented:
BTW, even though the ldapDisplayName of the attribute is "mail", specifying "EmailAddress" to the -Properties parameter will also work.  This is just an example of the Get-ADUser cmdlet doing some magic behind the scenes (basically providing some aliases for certain properties).  So the following will also work.
Import-Module ActiveDirectory
Get-ADUser -SearchBase "OU=people,dc=sanjosefoothillfamilycomm,dc=local" -Filter *  -Properties emailaddress |
  Select givenname, surname, emailaddress |
  Export-Csv "C:\Users\narce\Desktop\script1.csv" -notype

Open in new window

Author

Commented:
Both ObDa and footech worked, thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial