Export ADUser into a csv
I'm trying to get information form AD users and I can only get table format not..columnar csv type format.
I'm trying to get the first column in mailboxreports.csv (DisplayName) and look into AD and export into a new csv more information so I can import elsewhere, but the exported file must be csv format like excel. 

Import-Module ActiveDirectory
$Users = Import-CSV -Delimiter "," -Path "C:\Reports\MailboxReport.csv"

foreach ($User in $Users)
{
 $displayName = $User.DisplayName 
 get-AdUser -Filter {Name -eq $displayName} -Properties Name,Title,City,L,OfficePhone,MobilePhone | Out-File c:\Reports\ADInfo.csv -Append
}

when I try export-csv..it also does something funky. (plus append switch is not supported by export-csv)

Any ideas anyone?

May 26th, 2015 1:11pm

Read the help for these commands to understand how to use them.

Import-CSV C:\Reports\MailboxReport.csv |
    ForEach-Object{
        get-AdUser -Filter "Name -eq '$($_.displayName)'" -Properties Name,Title,City,L,OfficePhone,MobilePhone 
} |
    Select Name,Title,City,L,OfficePhone,MobilePhone |
    Export-Csv c:\Reports\ADInfo.csv 
Free Windows Admin Tool Kit Click here and download it now
May 26th, 2015 1:22pm

Thank you, but there is a error at position 10.

get-AdUser : Error parsing query: 'Name -eq Elia Jia' Error Message: 'syntax error' at position: '10'.
At line:3 char:9
+         get-AdUser -Filter "Name -eq $($_.DisplayName)" -Properties Name,Title,C ...

 Name after -eq is anyone.

I understand what your script is doing, but am confused.. don't you need to get the column name before you can get the data? How does the foreach know to get the data from that column. ( I did read the article on TechNet of which doesn't explain this scenario)

Brett

May 26th, 2015 2:17pm

Sorry - it needs to be like this:

Import-CSV C:\Reports\MailboxReport.csv |
    ForEach-Object{
        get-AdUser -Filter "Name -eq '$($_.displayName)'" -Properties Name,Title,City,L,OfficePhone,MobilePhone 
} |
    Select Name,Title,City,L,OfficePhone,MobilePhone |
    Export-Csv c:\Reports\ADInfo.csv 

Free Windows Admin Tool Kit Click here and download it now
May 26th, 2015 2:21pm

ah yes.. the single quote, like SQL syntax...

Thank you very much.
May 26th, 2015 2:34pm

Hi,

Instead of power shell , I can give you easy steps through GUI

Go Active Directory Users and computers-> select the OU want to export users list-> right click -> click View-> select Add/Remove Columns " Here you can add appropriate fields you want to export the user" -> then click ok

after it generate the info again right click - click Export list-> change Save As type to CSV -> put the file name and save.

Hope this info helps

Regards

Joby

Free Windows Admin Tool Kit Click here and download it now
May 26th, 2015 2:47pm

You are welcome.

May 26th, 2015 2:48pm

Hi,

Instead of power shell , I can give you easy steps through GUI

Go Active Directory Users and computers-> select the OU want to export users list-> right click -> click View-> select Add/Remove Columns " Here you can add appropriate fields you want to export the user" -> then click ok

after it generate the info again right click - click Export list-> change Save As type to CSV -> put the file name and save.

Hope this info helps

Regards

Joby

  • Proposed as answer by Joby M Chacko Tuesday, May 26, 2015 6:45 PM
Free Windows Admin Tool Kit Click here and download it now
May 26th, 2015 6:45pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics