# Expanding and selecting MemberOf along with other Properties

**URL:** <https://forums.powershell.org/t/expanding-and-selecting-memberof-along-with-other-properties/17599>\
**Category:** PowerShell Help\
**Created:** [September 22, 2021, 7:32pm UTC](https://forums.powershell.org/t/expanding-and-selecting-memberof-along-with-other-properties/17599 "2021-09-22T19:32:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff-taylor](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeff-taylor/32/1015_2.png) [@jeff-taylor](https://forums.powershell.org/u/jeff-taylor)\
**Post date:** [September 22, 2021, 7:32pm UTC](https://forums.powershell.org/t/expanding-and-selecting-memberof-along-with-other-properties/17599/1 "2021-09-22T19:32:45Z")

</div>

I want to gather not only attributes of a bunch of Users but also gather up their memberOf, expanded out in the same select statement, and output all to a csv

I have this

```auto
$MacUsers = (Import-Csv -Path "D:\MacOsUsers.csv")."Last User"

$Properties =
@(
'samAccountName'
'DistinguishedName'
'Manager'
'MemberOf'
'Name = 'memberOf'; Expression = { $_.MemberOf -Property -expandProperty MemberOf}'
'Title'

foreach ($MacUser in $MacUsers) {
    $MacUser = $MacUser
    Get-ADUser -identity $MacUser -Properties $Properties |
        select $Properties |
            Export-Csv -Path "D:\MacOS\AllMacUsers.csv" -Append -Force -NoTypeInformation
}

```

but get error: “Unexpected token ‘memberOf’”

How can I collect into a csv the whole kittenkaboodle?

thank you

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 22, 2021, 7:45pm UTC](https://forums.powershell.org/t/expanding-and-selecting-memberof-along-with-other-properties/17599/2 "2021-09-22T19:45:11Z")

</div>

If I got it right something like this should work:

```auto
$MacUsers = (Import-Csv -Path "D:\MacOsUsers.csv")."Last User"

$Properties = @(
    'samAccountName'
    'DistinguishedName'
    'Manager'
    @{Name = 'MemberOf'; Expression = { $_.MemberOf -join ';'}}
    'Title'
)
$Result = 
foreach ($MacUser in $MacUsers) {
    Get-ADUser -identity $MacUser -Properties Manager,MemberOf,Title |
    Select-Object -Property $Properties 
}
$Result |
Export-Csv -Path 'D:\MacOS\AllMacUsers.csv' -NoTypeInformation

```

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [September 22, 2021, 8:21pm UTC](https://forums.powershell.org/t/expanding-and-selecting-memberof-along-with-other-properties/17599/3 "2021-09-22T20:21:56Z")

</div>

Same script as Olaf’s just rewritten to use the pipeline. The main issue I wrote this to “solve” is the possible repeat opening/closing of the CSV.

```auto
$Properties = @(
    'samAccountName'
    'DistinguishedName'
    'Manager'
    'MemberOf'
    @{Name = 'memberOf'; Expression = { $_.MemberOf -join ';'}}
    'Title'
)

Import-Csv -Path "D:\MacOsUsers.csv" | ForEach-Object {
    Get-ADUser -identity $_."Last User" -Properties * |
        Select-Object -Property $Properties
} | Export-Csv -Path "D:\MacOS\AllMacUsers.csv" -NoTypeInformation -Force

```

There are a number of ways to solve it without using the pipeline as well. Such as just collecting all the output first

```auto
$MacUsers = (Import-Csv -Path "D:\MacOsUsers.csv")."Last User"

$Properties = @(
    'samAccountName'
    'DistinguishedName'
    'Manager'
    'MemberOf'
    @{Name = 'memberOf'; Expression = { $_.MemberOf -join ';'}}
    'Title'
)

$output = foreach ($MacUser in $MacUsers) {
    Get-ADUser -identity $MacUser -Properties * |
        Select-Object -Property $Properties
}

$output | Export-Csv -Path "D:\MacOS\AllMacUsers.csv" -Force -NoTypeInformation

```

As always, great answer Olaf. 🙂

---

<div class="post-metadata">

**Author:** ![jeff-taylor](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeff-taylor/32/1015_2.png) [@jeff-taylor](https://forums.powershell.org/u/jeff-taylor)\
**Post date:** [September 22, 2021, 9:32pm UTC](https://forums.powershell.org/t/expanding-and-selecting-memberof-along-with-other-properties/17599/4 "2021-09-22T21:32:35Z")

</div>

Testing your code Olaf I get csv and good values in all but the MemberOf column

```auto
MemberOf
Microsoft.ActiveDirectory.Management.ADPropertyValueCollection
Microsoft.ActiveDirectory.Management.ADPropertyValueCollection
Microsoft.ActiveDirectory.Management.ADPropertyValueCollection
Microsoft.ActiveDirectory.Management.ADPropertyValueCollection

```

---

<div class="post-metadata">

**Author:** ![jeff-taylor](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeff-taylor/32/1015_2.png) [@jeff-taylor](https://forums.powershell.org/u/jeff-taylor)\
**Post date:** [September 22, 2021, 9:37pm UTC](https://forums.powershell.org/t/expanding-and-selecting-memberof-along-with-other-properties/17599/5 "2021-09-22T21:37:54Z")

</div>

Same for Doug’s…good data, save for the MemberOf Column

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 22, 2021, 9:50pm UTC](https://forums.powershell.org/t/expanding-and-selecting-memberof-along-with-other-properties/17599/6 "2021-09-22T21:50:35Z")

</div>

> [@jeff-taylor](#):
>
> Testing your code Olaf

… hmmm … changed my code suggestion from above slightly … .try it now.

---

<div class="post-metadata">

**Author:** ![jeff-taylor](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeff-taylor/32/1015_2.png) [@jeff-taylor](https://forums.powershell.org/u/jeff-taylor)\
**Post date:** [September 22, 2021, 10:59pm UTC](https://forums.powershell.org/t/expanding-and-selecting-memberof-along-with-other-properties/17599/7 "2021-09-22T22:59:50Z")

</div>

> [@Olaf](#):
>
> ```auto
> $MacUsers = (Import-Csv -Path "D:\MacOsUsers.csv")."Last User"
> 
> $Properties = @(
> 'samAccountName'
> 'DistinguishedName'
> 'Manager'
> @{Name = 'MemberOf'; Expression = { $_.MemberOf -join ';'}}
> 'Title'
> )
> $Result = 
> foreach ($MacUser in $MacUsers) {
> Get-ADUser -identity $MacUser -Properties Manager,MemberOf,Title |
> Select-Object -Property $Properties 
> }
> $Result |
> Export-Csv -Path 'D:\MacOS\AllMacUsers.csv' -NoTypeInformation
> 
> ```

That’s working for the MemberOfs now thank you…but if I want to add more extensionAttributes (say, 15 more) to that $Properties variable, I was hoping to get those too (without adding them _individually_ to $MacUser -Properties Manager,MemberOf, Title, etc etc)

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 22, 2021, 11:40pm UTC](https://forums.powershell.org/t/expanding-and-selecting-memberof-along-with-other-properties/17599/8 "2021-09-22T23:40:05Z")

</div>

> [@jeff-taylor](#):
>
> but if I want to add more extensionAttributes (say, 15 more) to that $Properties variable, I was hoping to get those too (without adding them _individually_ to $MacUser -Properties Manager,MemberOf, Title, etc etc)

Most of the time we try to minimize the stress you put on your AD by specifying the properties you’re after explicitly. But if you really need 15 more of them - what sounds a kind of really weird to me - you may use the asterisk ( \* ) instead. 😉

---

<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:21pm UTC](https://forums.powershell.org/t/expanding-and-selecting-memberof-along-with-other-properties/17599/9 "2024-05-16T20:21:20Z")

</div>


