none
Export ADUser into a csv RRS feed

  • Question

  • 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?

    Tuesday, May 26, 2015 5:10 PM

Answers

  • 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 


    \_(ツ)_/

    Tuesday, May 26, 2015 6:19 PM

All replies

  • 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 


    \_(ツ)_/




    • Edited by jrv Tuesday, May 26, 2015 6:21 PM
    Tuesday, May 26, 2015 5:20 PM
  • 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

    Tuesday, May 26, 2015 6:16 PM
  • 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 


    \_(ツ)_/

    Tuesday, May 26, 2015 6:19 PM
  • ah yes.. the single quote, like SQL syntax...

    Thank you very much.
    Tuesday, May 26, 2015 6:32 PM
  • 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

    Tuesday, May 26, 2015 6:45 PM
  • You are welcome.


    \_(ツ)_/

    Tuesday, May 26, 2015 6:46 PM