Exporting member info of multiple adgroups using import-csv RRS feed

  • Question

  • I seem to always run into something when I try to use import-csv. My goal here is to get all of the ADUsers from multiple ADGroups and export that info to csv. My import csv, for example, is one column named 'Group'. I'd like the export to have 3 columns.. 'Group' 'ID' 'Name'. And to list the name of the first group (from import) in column A until the last member and then the name of the second group (from import) in column A next... so on and so forth. What I have currently get's all the user info the way that I want it but leaves column A blank so I can't decifer it. Below is my code so far:

    $Groups = Import-Csv {file path}\GroupImport.csv
    $Groups |
    % {Get-ADGroupMember $_.Group} |
    select name |
    % {Get-ADUser -Identity $ -Properties * |
    select @{N="Group";E={$Group}}, @{N="ID";E={$}}, @{N="Name";E={$_.surname + ", " + $_.givenName}} } |
    Export-Csv {file path}\GroupExport.csv -NoTypeInformation

    Any advice or point in the right direction would help appreciated!

    Friday, June 10, 2016 4:49 PM


  • Hi,

    This is why I like ForEach-Object loops. Nice and simple.

    Import-Csv .\groupList.csv | ForEach {
        $groupName = $_.Group
        Get-ADGroupMember -Identity $groupName | 
            Get-ADUser |
                Select @{N='Group';E={$groupName}},@{N='ID';E={$_.Name}},@{N='Name';E={"$($_.Surname), $($_.GivenName)"}}
    } | Export-Csv .\groupMembers.csv -NoTypeInformation

    • Marked as answer by PS_Novice1982 Friday, June 10, 2016 6:23 PM
    Friday, June 10, 2016 5:42 PM

All replies