PowerShell Script

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"
FCHCAdminAsked:
Who is Participating?
 
oBdACommented:
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

1
 
oBdACommented:
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

0
 
FCHCAdminAuthor 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
0
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
oBdACommented:
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

0
 
FCHCAdminAuthor Commented:
That was my original script (in the problem description) and is not working. That's the problem
0
 
footechCommented:
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

1
 
FCHCAdminAuthor Commented:
Both ObDa and footech worked, thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.