locked
Get members list from multiple AD groups RRS feed

  • Question

  • Hi,

    Please help me out. What I'm doing is importing 16 AD groups in a PowerShell script and finding the list of users in those AD groups. I am doing this for audit purpose. 

    Since I'm exporting the user list for 16 AD groups, the -append parameter is making the list of users in the output file appear one next to it and I couldn't figure out which user belongs to which group, since there are 450 to 500 users in the output file. I need some changes in the below script so that each user appears with the group name for which it is extracted. 

    Example- script run for first AD group- lists 4 users in that AD group. The script runs again for another AD group, this should display with the users' list along with the Group name which is trying to export the list. Hope I am not complicating it. Please help me out.


    $groups = Get-content -path "C:\Users\Santosh\OBIP.csv"
    foreach ($GroupName in $groups) 
    {
    Get-ADgroupmember -Id $GroupName -Recursive | Where objectclass -eq 'user' | Get-ADUser -Properties Displayname,GivenName,Surname,UserPrincipalName,Title,Department,Enabled,ObjectClass, memberof | Select DistinguishedName, samAccountName, Name, Displayname, GivenName, Surname, UserPrincipalName, Title, Department, Enabled, ObjectClass | export-csv -path "C:\Users\Santosh\OBIP\GN.csv" -Append
    }
    Monday, August 6, 2018 3:59 PM

All replies

  • Hi,

    Please help me out. What I'm doing is importing 16 AD groups in a <g class="gr_ gr_1165 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="1165" id="1165">powershell</g> script and finding the list of users in those AD groups. I am doing this for audit purpose. 

    Since I'm exporting the user list for 16 AD groups, the -append parameter is making the list of users in the output file appear one next to it and I couldn't figure out which user belongs to which group, since there are 450 to 500 users in the output file. I need some changes in the below <g class="gr_ gr_1166 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-del replaceWithoutSep" data-gr-id="1166" id="1166">script,</g> so that each user appears with the group name for which it is extracted. 

    Example- script run for first AD group- lists 4 users in that AD group. The script runs again for another AD group, this should display with the <g class="gr_ gr_1170 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" data-gr-id="1170" id="1170">users</g> list along with the Group name from which it is trying to export the list. Hope I am not complicating it. Please help me out.


    $groups = Get-content -path "C:\Users\Santosh\Documents\OBIP.csv"
    foreach ($GroupName in $groups) 
    {
    Get-ADgroupmember -Id $GroupName -Recursive | Where objectclass -eq 'user' | Get-ADUser -Properties Displayname,GivenName,Surname,UserPrincipalName,Title,Department,Enabled,ObjectClass, memberof | Select DistinguishedName, samAccountName, Name, Displayname, GivenName, Surname, UserPrincipalName, Title, Department, Enabled, ObjectClass | export-csv -path "C:\Users\Santosh\Documents\OBIP\GN.csv" -Append
    }

    • Merged by jrv Monday, August 6, 2018 4:27 PM DUPLICATE
    Monday, August 6, 2018 3:57 PM
  • <Fixed the post as it was unreadable ; thanks to jrv>

    Have this checked, this should work.

    Either put the group names in .txt file and use the variable "$groups = Get-Content C:\grouplist.txt"

    OR

    Just give the Group names like below in my script.

    ===================================================

    $groups = "Group1", "Group2", "Group3","Group4"
    
    $results = foreach ($group in $groups) {
        Get-ADGroupMember $group | select samaccountname, name, @{n='GroupName';e={$group}}, @{n='Description';e={(Get-ADGroup $group -Properties description).description}}
    }
    
    $results
    
    $results | Export-csv C:\GroupMemberShip.txt -NoTypeInformation

    ====================================

    **Mark as answer if this helps you, so that it will help others who have same query**


    Monday, August 6, 2018 4:22 PM
  • Please fix your post it is unreadable.

    Please post code using the code posting tool provided.


    \_(ツ)_/

    Monday, August 6, 2018 4:23 PM
  • <Fixed the post as it was unreadable ; thanks to jrv>

    Have this checked, this should work.

    Either put the group names in .txt file and use the variable "$groups = Get-Content C:\grouplist.txt"

    OR

    Just give the Group names like below in my script.

    ===================================================

    $groups = "Group1", "Group2", "Group3","Group4"
    
    $results = foreach ($group in $groups) {
        Get-ADGroupMember $group | select samaccountname, name, @{n='GroupName';e={$group}}, @{n='Description';e={(Get-ADGroup $group -Properties description).description}}
    }
    
    $results
    
    $results | Export-csv C:\GroupMemberShip.txt -NoTypeInformation

    ====================================

    **Mark as answer if this helps you, so that it will help others who have same query**


    **Mark as Answered if this helps you, so that it will help others who have same query**
    Friday, August 10, 2018 6:09 PM
  • Is there any update for this issue, if the reply is helpful to you, please try to mark it as an answer, it will help others who have the similar issue.

    Neeranjan Shettar (MyPage)

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Wednesday, September 26, 2018 11:39 AM
  • Is there any update for this issue, if the reply is helpful to you, please try to mark it as an answer, it will help others who have the similar issue.

    Neeranjan Shettar (MyPage)

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Saturday, October 6, 2018 10:09 AM
  • Is there any update for this issue, if the reply is helpful to you, please try to mark it as an answer, it will help others who have the similar issue.

    Neeranjan Shettar (MyPage)

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Friday, October 19, 2018 7:46 PM
  • Stop begging for points.

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

    Friday, October 19, 2018 9:37 PM