none
Export powershell changes to csv format RRS feed

  • Question

  • I am running this script to add all employee IDs into my active directory 

    >> Import-Module ActiveDirectory
    >> $Users = Import-Csv -Path C:\excelfiles\importtest.csv
    >> Get-Content -Path C:\excelfiles\importtest.csv | ft
    >> sleep 10
    >> foreach ($User in $Users)
    >> {
    >> Set-ADUser -identity $User.SamAccountName -replace @{"employeeID" = $User.employeeID} -verbose
    >> } 
    >> $results | Export-Csv -path c:\excelfiles\export.csv

    The last part of it is my attempt to pull out the results into a csv file but no luck, it exports the file but with no data. Any idea of what im doing wrong? Ive also tried it with the script below was and same results.....

    >>Import-Module ActiveDirectory
    >>$Users = Import-Csv -Path C:\excelfiles\importtest.csv
    >>Get-Content -Path C:\excelfiles\importtest.csv | ft
    >>sleep 10
    >>foreach ($User in $Users)
    >>{
    >>Set-ADUser -identity $User.SamAccountName -replace @{"employeeID" = $User.employeeID} -verbose | Export-Csv -path >>C:\excelfiles\export.csv -Append
    >>}



    Tuesday, September 3, 2019 10:29 PM

All replies

  • Please post your code correctly in this forum: How to post code in Technet Forums


    \_(ツ)_/

    Tuesday, September 3, 2019 11:20 PM
    Moderator
  • Because you haven't put anything into the "$results" variable you're exporting nothing into the CSV file.

    In your second example, the Verbose stream isn't what you think it is -- it's a separate data stream (#4).

    What, exactly, do you want to see in that exported data set?


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Wednesday, September 4, 2019 2:51 AM
  • Learning PowerShell before trying to write a script would be the most important step to take.

    Getting Started with Microsoft PowerShell

    By default Set-AdUser has no output so it cannot be exported.

    When using a CmdLet and you cannot understand what is happening always read the full help for the CmdLet.

    help Set-AdUser -online


    \_(ツ)_/


    • Edited by jrvModerator Wednesday, September 4, 2019 3:08 AM
    Wednesday, September 4, 2019 3:05 AM
    Moderator
  • First of all, you are not assigning output of Set-ADUser command output to $result variable. Second thing, I am npt able to understand, why it is exporting to a CSV file, the result of Set-ADUser is not that formatted output which will be a properly visualized using CSV file. 
    Wednesday, September 4, 2019 3:27 AM
  • First of all, you are not assigning output of Set-ADUser command output to $result variable. Second thing, I am npt able to understand, why it is exporting to a CSV file, the result of Set-ADUser is not that formatted output which will be a properly visualized using CSV file. 

    s noted above - Set-AdUser has no output so it cannot be assigned to anything or exported to a CSV or any other file.

    Please read the help for the CmdLet to learn how it works.  Guessing just adds confusion for new users.  

    Properly formatting code is the first step to understanding how it is working.  Sloppy, badly formatted code is impossible to understand or manage.

    Always learn PowerShell basics form a reliable source or book before trying to write scripts.


    \_(ツ)_/

    Wednesday, September 4, 2019 3:32 AM
    Moderator
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee



    Just do it.

    Friday, October 4, 2019 8:52 AM
    Moderator