Getting a list of users and their AD groups with groups in csv list, 90% there... RRS feed

  • Question

  • I have a powershell query that looks like this:

    (get-aduser -Filter ('SamAccountName -like "Matthew*" -or Name -like "Matthew*"') -Properties MemberOf | Select-Object SamAccountName, Name, UserPrincipalName, MemberOf)

    The output looks something like this:

    What I am trying to do is use some sort of replace or regex function  so that the MemberOf list is an easy to read list and gets rid of all the


    So that is looks like a comma seperated list like this:

    someCN,someOtherCN,someother3rdCN   (basically just the CN names)

    "Corporate, IT-Group, Billing"  something like that

    I just can't figure it out and feel like I have tried every combination.  banging my head on the wall for a couple hours now.


    • Edited by mschandler Thursday, February 8, 2018 4:49 PM clarify
    Thursday, February 8, 2018 4:31 PM

All replies

  • | select -ExpandProperty memberof

    Will format like so:


    Hope this helps. Please remember to use the code pasting cool when posting code.

    Thursday, February 8, 2018 4:36 PM
  • So I had tried that previously and couldn't get it to work I guess for 2 reasons?  I'm using Select-Object and also I am selecting a couple different properties.

    Maybe some sort of regex or something just to get the cn names parsed from that?

    Also I added in the code pasting tool, thank's for the reminder.


    Thursday, February 8, 2018 4:47 PM
  • (Get-ADPrincipalGroupMembership).name will being back all the groups in a nice to read manner. Could create a loop that uses the samaccountname of the user to search for the groups this way?

    I'm not very good with Regex. Maybe a Google or someone else can help :)

    Thursday, February 8, 2018 5:21 PM
  • Get-AdUser -Filter ('SamAccountName -like "Matthew*" -or Name -like "Matthew*"') -Properties MemberOf | Select-Object SamAccountName, Name, UserPrincipalName, @{e={(Get-ADPrincipalGroupMembership -identity $_.Name).name};L='MemberOf'}
    The above code will suffice your requirement.

    Regards kvprasoon

    Thursday, February 8, 2018 7:02 PM