none
Powershell Export-csv I get numbers instead of text (group members export)

    Question

  • Hello,

    I have the task to find groups containing some word (for example application) , and then list all the members of these groups. In the end I need to export it to .csv file. To make things a little harder i want only the sam account names in the file. I found a script and slightly modified it to my needs - looks like that :

    $groups = Get-QADGroup | Where {$_.name -like "*application*"}
    $membes=foreach ($group in $groups) {
        get-qaduser -memberof $group |foreach{$_.samaccountname}
    }
    $membes | Export-Csv file.csv -NoTypeInformation

    When I do it without exporting everything looks fine I get the list of users , but when I use export-csv i only get bunch of numbers instead of the names. The number in each row is equal to the number of letters in users name. If I use out-file instead of export-csv - again everything is ok. So whats wrong with export-csv ?

    I already resolved the task with out-file but i'm really curious why it doesn't work as I wanted it in thefirst place. Thanks in advance.

    Tuesday, January 29, 2013 8:21 AM

Answers

  • Try:

    $groups = Get-QADGroup | Where {$_.name -like "*application*"} $members=foreach ($group in $groups) { get-qaduser -memberof $group | select samaccountname }

    $members | Export-Csv file.csv -NoTypeInformation



    ¯\_(ツ)_/¯


    Tuesday, January 29, 2013 9:53 AM

All replies

  • Try:

    $groups = Get-QADGroup | Where {$_.name -like "*application*"} $members=foreach ($group in $groups) { get-qaduser -memberof $group | select samaccountname }

    $members | Export-Csv file.csv -NoTypeInformation



    ¯\_(ツ)_/¯


    Tuesday, January 29, 2013 9:53 AM
  • I got the error about empty pipeline as earlier (I'm not sure why ,but that is why i used Variable $membes)  so I used that :

    $groups = Get-QADGroup | Where {$_.name -like "*appl*"}
    $membes=foreach ($group in $groups) {
        get-qaduser -memberof $group | select samaccountname
    } 
    $membes| Export-Csv file.csv -NoTypeInformation

    But It actually worked - thank you. But could you explain why it didn't work with foreach ?

    Tuesday, January 29, 2013 11:39 AM
  • Regarding foreach construct: it does not write things to the pipeline by defualt (unlike foreach-object cmdlet). You can either use variable (as you did) or put whole expression in subexpression: $(forech ...) | Export-Csv

    Regarding your first export: Export-Csv works on objects, and you passed strings. This object has single property, length, that is number. Rings any bells... ? ;)

    jrv passes object with single property (samaccountname), thus export will contain list of samaccountnames.

    Friday, March 15, 2013 8:51 AM
  • my example was put together quickly and I just added the export without thinking.

    Bartek is correct.  foreach() does not output to pipeline but can be assigned $results=foreach(...){....}.  It is good you thought to try that.

    I will fix my example.


    ¯\_(ツ)_/¯

    Friday, March 15, 2013 9:00 AM
  • Normally I would write that like this:

    Get-QADGroup | Where {$_.name -like "*application*"} |
         ForEach-Object{
              get-qaduser -memberof $group | select samaccountname
         } | Export-Csv file.csv -NoTypeInformation
    
    

    I decided to flatten it out for you so it would be more understandable but forgot to do the assignment when I converted.

    I generally just type most things as a pipeline because that I an easier way for me to think.


    ¯\_(ツ)_/¯


    • Edited by jrv Friday, March 15, 2013 9:05 AM
    Friday, March 15, 2013 9:04 AM