none
Export Usergroup Membership Powershell RRS feed

  • Question

  • This is what im looking forward to get but it has been hard for me since im litteraly new in Powershell

    Group             Accountname 
    group1            user1 
    group1            user2 
    group1            user3 
    group2            user4 
    group2            user5 
    group3            user6 
    group3            user7 
    group3            user8 
    group4            user9 
    group5            user10 
    Thursday, November 14, 2019 9:16 PM

All replies

  • I found this:

    Get-ADUser -Filter * -Properties DisplayName,memberof | % {
      New-Object PSObject -Property @{
    	UserName = $_.DisplayName
    	Groups = ($_.memberof | Get-ADGroup | Select -ExpandProperty Name) -join ","
    	}
    } | Select UserName,Groups | Export-Csv C:\temp\report.csv -NTI

    and is giving me this:

    Username Groups
    TomEA IPO-File Server Admins, File Server Admins, Group Policy Creator Owners, Enterprise Admins
    Thomas H. Ploskon IPO-IT, IT, Folder Redirection Users,
    Template IPO IPO-customers, customers, Folder Redirection Users,
    Andre Granadeiro IPO-customers, customers, finance, Managers,
    Cesar Masforroll IPO-IT, IT, Folder Redirection Users


    still is not giving me what i need, User's Groups are coming to the user but with comma (,) i need the groups to be repeated when more than an user is in it instead of have all the groups under a user sapareted with comma. 

    Thursday, November 14, 2019 9:23 PM
  • If you don't want commas then don't use a comma in the "-join".

    Just keep in mind that for each user, every group will be placed into the single column "Groups".


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

    Thursday, November 14, 2019 10:24 PM
  • Everyone always tries to do this the hard way leftover from PS v2

    Here is how we do it now:

    Get-AdGroup -Filter * -PipelineVariable grp |
        Get-ADGroupMember |
        select @{n='Group';e={$grp.Name}},SamAccountName
    Learn how to use the pipeline and it will greatly simplify almost all of your scripts issues.


    \_(ツ)_/

    Thursday, November 14, 2019 11:35 PM
    Moderator
  • Once you learn the pipeline you can do fancy things like this.

    Get-AdGroup -Filter * -PipelineVariable grp | 
        Get-ADGroupMember |
        Select-Object @{n='Group';e={$grp.Name}},@{n='AccountName';e={$_.SamAccountName}} |
        Format-Table -GroupBy Group


    \_(ツ)_/

    Thursday, November 14, 2019 11:41 PM
    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


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, December 6, 2019 9:01 AM
    Moderator