Asked by:
Getting a list of users and their AD groups with groups in csv list, 90% there...

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
{CN=someCN,OU=asdf,DC=someDC,CN=someOtherCN,OU=someOtherOU,DC=someOtherDC,CN=someother3rdCN....}
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.
Thanks!
Matt
- 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:
"CN=CNNAME,OU=OUNAME,DC=DCNAME"
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.
Matt
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 :)
- Edited by I.T Delinquent Thursday, February 8, 2018 5:26 PM
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
- Edited by PRASOON KARUNAN V Thursday, February 8, 2018 7:03 PM
- Proposed as answer by Albert LingMicrosoft contingent staff Friday, February 9, 2018 3:06 AM
Thursday, February 8, 2018 7:02 PM