# Name/Expression -expand MemberOf

**URL:** <https://forums.powershell.org/t/name-expression-expand-memberof/6448>\
**Category:** PowerShell Help\
**Created:** [May 13, 2016, 3:23pm UTC](https://forums.powershell.org/t/name-expression-expand-memberof/6448 "2016-05-13T15:23:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![grant-harrington](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@grant-harrington](https://forums.powershell.org/u/grant-harrington)\
**Post date:** [May 13, 2016, 3:23pm UTC](https://forums.powershell.org/t/name-expression-expand-memberof/6448/1 "2016-05-13T15:23:19Z")

</div>

I’ve got the following Name/Expression working to list only the CN of each group this user is a member of.

```
$WIPMemberOf = @{n="MemberOf";e={($_ | Select -ExpandProperty MemberOf) | Where {$_ -match "CN=(.*?),"} | ForEach{$Matches[1]}}}
```

```
$ADUC_Users | where {$_.samaccountname -like 'GRANT.HARRINGTON'} | Select $WIPMemberOf | fl
```

The only remaining issue is the output is still listed in curly braces.

MemberOf: {Group01,Group02,SomeOtherGroup…}

I’m not sure where to place -join ‘,’ as I’m hoping to get it output like this:

MemberOf: Group01,Group02,SomeOtherGroup,LastGroup

---

<div class="post-metadata">

**Author:** ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)\
**Post date:** [May 13, 2016, 7:05pm UTC](https://forums.powershell.org/t/name-expression-expand-memberof/6448/2 "2016-05-13T19:05:50Z")

</div>

This is an intriguing way of doing this, but there ya go.

```
$WIPMemberOf = @{n="MemberOf";e={(($_ | Select -ExpandProperty MemberOf) | Where {$_ -match "CN=(.*?),"} | ForEach{$Matches[1]}) -join ","}}
```

---

<div class="post-metadata">

**Author:** ![grant-harrington](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@grant-harrington](https://forums.powershell.org/u/grant-harrington)\
**Post date:** [May 14, 2016, 1:22am UTC](https://forums.powershell.org/t/name-expression-expand-memberof/6448/3 "2016-05-14T01:22:56Z")

</div>

That worked nicely. Thank you.

My reason for doing it this way is that when I have multiple properties that have a expandable attribute, I can create multiple name/expression variables and view only the “name” (CN).

Using your response I was able to quickly achieve this:  
MemberOf: Group01,Group02,SomeOtherGroup,LastGroup  
DirectReports: User01, User02, LastUser

If you have a suggestion for another way to achieve the same, I’d be interested in that.

Thanks again.

---

<div class="post-metadata">

**Author:** ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)\
**Post date:** [May 14, 2016, 4:05am UTC](https://forums.powershell.org/t/name-expression-expand-memberof/6448/4 "2016-05-14T04:05:51Z")

</div>

Hey Grant,  
What makes your approach interesting is the use of the predefined hash table variable $WIPMemberOf to use to calculate the value in the select-object. I had just never seen that approach before. With that said; however, this approach does end up breaking your information into multiple objects of just name and value as opposed to one object that can use the rest of the powershell cmdlets to control the presentation. Let me give an example of what I mean.

You can use Select object from your Get-ADUser results to just get the properties you want and create custom properties if needed.

```
Get-ADUser -filter {samaccountname -eq 'user1 '} -Properties MemberOf,DirectReports |
Select-Object SamAccountName,
              @{Label = 'MemberOf'; Expression = {($_.MemberOf | ForEach-Object {([regex]"CN=(.*?),").match($_).Groups[1].Value}) -join ","}},
              @{Label = 'DirectReports'; Expression = {($_.DirectReports | ForEach-Object {([regex]"CN=(.*?),").match($_).Groups[1].Value}) -join ","}}
```

**Results:**

```
SamAccountName MemberOf DirectReports                              
-------------- -------- -------------                             
user1 WindowsAdmins,CA Power Users export,Power 2. User,Power 1. User
```

Since this keeps all of the data in a single object, you can use the formats to change how the data is displayed

```
Get-ADUser -filter {samaccountname -eq 'user1 '} -Properties MemberOf,DirectReports |
Select-Object SamAccountName,
              @{Label = 'MemberOf'; Expression = {($_.MemberOf | ForEach-Object {([regex]"CN=(.*?),").match($_).Groups[1].Value}) -join ","}},
              @{Label = 'DirectReports'; Expression = {($_.DirectReports | ForEach-Object {([regex]"CN=(.*?),").match($_).Groups[1].Value}) -join ","}} |
Format-List
```

**Results:**

```
SamAccountName : user1 
MemberOf : WindowsAdmins,CA Power Users
DirectReports : export,Power 2. User,Power 1. User
```

You can even export this using the output or export cmdlets like Export-CSV or Export-CliXML

```
Get-ADUser -filter {samaccountname -eq 'user1 '} -Properties MemberOf,DirectReports |
Select-Object SamAccountName,
              @{Label = 'MemberOf'; Expression = {($_.MemberOf | ForEach-Object {([regex]"CN=(.*?),").match($_).Groups[1].Value}) -join ","}},
              @{Label = 'DirectReports'; Expression = {($_.DirectReports | ForEach-Object {([regex]"CN=(.*?),").match($_).Groups[1].Value}) -join ","}} |
Export-Csv c:\temp\export.csv
```

As an additional option, you can also do a custom powershell object rather than using select-object if it make more sense, and gives you the same ability to pipe into other powershell cmdlets for additional processing.

```
Get-ADUser -filter {samaccountname -eq 'user1 '} -Properties MemberOf,DirectReports |
ForEach-Object {
    [pscustomobject]@{
        SamAccountName = $_.samaccountname
        MemberOf = ($_.MemberOf | ForEach-Object {([regex]"CN=(.*?),").match($_).Groups[1].Value}) -join ","
        DirectReports = ($_.DirectReports | ForEach-Object {([regex]"CN=(.*?),").match($_).Groups[1].Value}) -join ","
    }
}
```

---

<div class="post-metadata">

**Author:** ![grant-harrington](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@grant-harrington](https://forums.powershell.org/u/grant-harrington)\
**Post date:** [May 14, 2016, 4:31am UTC](https://forums.powershell.org/t/name-expression-expand-memberof/6448/5 "2016-05-14T04:31:51Z")

</div>

Thank you for that insight. I’m a fan of custom objects for my projects whenever they make logical sense to use them, and may go that route.

Biggest reason for the Select $somevariable, is so I can group attributes based on the GUI interface for record keeping

$General = select attributes that are on the General tab of ADUC  
$Address = select attributes that are on the Address tab of ADUC  
etc…

Thank you for the other options provided (and solving the original question I had), it gives me some flexibility in my scripts.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:41pm UTC](https://forums.powershell.org/t/name-expression-expand-memberof/6448/6 "2024-05-16T20:41:23Z")

</div>


