locked
How to get all groups and members of a nested group into csv format with Export-Csv RRS feed

  • Question

  • Hello

    I am new to Powershell scripting so i need a little help :)

    I have a AD Group which have many nested groups with members in each group

    I want to get a csv file with name of the nested group inside a group and the name of the member in the nested groups with their emai address

    I have tried with PS script under but i am not able to get file formated correctly with one colum for group name and one for name and one for email so i can import it into Excel

    get-ADGroupMember "ADgroup" | ?{$_.ObjectClass -eq "Group"} | %{Write-output $_.Name;Get-ADGroupMember $_ | Select-Object Name} |format-list | Out-File c\temp\groups.csv

    If anyone could help me i would appreciate it.

    Morten


    mh

    Thursday, July 4, 2019 9:10 PM

All replies

  • Try this might work out, if not omit expanding the name property - this will output the filename as dn

    Get-ADGroupMember -Identity rootgroup | ? {$_.objectclass -eq "group"} | select -ExpandProperty name | foreach { Get-ADGroupMember -Identity $_ | Get-ADUser -Properties mail | select name,mail | Export-Csv -NoTypeInformation -Path ~\desktop\$_}

    You could use Get-ADGroupmember  with the -recurse parameter, however this won't tell you which nested group they belong in

    Thursday, July 4, 2019 9:51 PM
  • Hello

    Thanks for your fast response

    I get this error when i run your script:

    Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "group name" to type "System.Char". Error: "String must be exactly one character long Name}

    The script command i am running is:

    Get-ADGroupMember -Identity "group name" | ? {$_.objectclass -eq "group"} | select -ExpandProperty name | foreach { Get-ADGroupMember -Identity $_ | Get-ADUser -Properties mail | select name,mail | Export-Csv "C:\temp\Members.csv"  -NoTypeInformation -Encoding UTF8 $_}


    mh

    Thursday, July 4, 2019 11:28 PM
  • To get all members of a group and all nested groups use this command:

    Get-ADGroupMember 'group name' |
        Get-ADUser -Properties Mail |
        Select-Object name, mail | 
        Export-Csv C:\temp\Members.csv -NoTypeInformation

    Learn to format code correctly so you can understand what it is doing.  Be careful of answers when the poster post code that is formatted naively and where the code seems to be a bit irrational.  Many people post answers by copying code and making changes but they never test their guesses.

    The safest way around this is to learn basic PowerShell and not rely on others who know less than you know.


    \_(ツ)_/

    Friday, July 5, 2019 12:49 AM
  • Hello

    Thank you so much for your help :)

    I am now able to get all the member name and email of all the nested groups in a group

    I needed to add -recursive after Get-ADGroupMember to receive that

    There is only one thing missing i need to get the name of the nested groups the users belong to in the root group

    Get-ADGroupMember "group name" -recursive | Get-ADUser -Properties Mail | Select-Object name, mail | Export-Csv C:\temp\Members.csv -NoTypeInformation

    Morten


    mh

    Friday, July 5, 2019 5:57 AM
  • Add the pipeline variable to the first command and use a computed property to add it to the select statement.

    help  about_CommonParameters

    help Select-Object -online


    \_(ツ)_/

    Friday, July 5, 2019 6:26 AM
  • You're passing the groupname variable to the -Delimiter parameter

    -Path and -Delimiter are both positional parameters path being position 0 and delimiter position 1

    Get-Help Export-Csv -Parameter path
    Get-Help Export-Csv -Parameter Delimiter

    Get-ADGroupMember -Identity rootgroup | `
    Where-Object { $_.objectclass -eq "group" } | `
    Select-Object -ExpandProperty name | `
    ForEach-Object{
        Get-ADGroupMember -Identity $_ | `
        Get-ADUser -Properties mail | `
        Select-Object name,mail | `
        #This Generates CSV named as the group for each group containg members
        Export-Csv -NoTypeInformation -Path ~\desktop\$_.csv
    }

    

    This should fit your requirement to find members of the nested groups.



    Friday, July 5, 2019 10:19 AM
  • Hello

    Almost there now :)

    I want to get the name of the nested groups into one csv file not a csv file for each nested group

    Morten


    mh

    Friday, July 5, 2019 11:01 AM
  • This can only be done with a recursive function.

    \_(ツ)_/

    Friday, July 5, 2019 12:04 PM
  • Hello

    Thanks i need to learn some more Powershell  now :)

    Morten


    mh

    Monday, July 8, 2019 10:38 AM
  • 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.

    Wednesday, July 31, 2019 8:31 AM