# Export Users Member of to Excel

**URL:** <https://forums.powershell.org/t/export-users-member-of-to-excel/11793>\
**Category:** PowerShell Help\
**Created:** [December 14, 2018, 4:24am UTC](https://forums.powershell.org/t/export-users-member-of-to-excel/11793 "2018-12-14T04:24:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ggman](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@ggman](https://forums.powershell.org/u/ggman)\
**Post date:** [December 14, 2018, 4:24am UTC](https://forums.powershell.org/t/export-users-member-of-to-excel/11793/1 "2018-12-14T04:24:23Z")

</div>

Hi

I am new to Powershell and I am a bit stuck on looking up 200 staff AD accounts via ‘SamAccountName’ and exporting their access groups (under ‘Member Of’), too an excel sheet in a clean format.

So if staff come back and need access to all the groups again (or some), I would like to look up User ID (SamAccountName) in the excel sheet and just copy data back into AD (or maybe just one or two groups need to be added back in).

This is what I have at the moment but I would like exported csv to have USer ID (SamAccountName) in one Column and access groups in another.

I have Googled; tried various formats and now my mind has just gone blank!

$userlist = Get-Content ‘C:\GS\Get Group List\User.txt’

Get-ADUser -Filter ‘\*’ -Properties memberof | Where-Object {$userlist -contains $_.SamAccountName  
} | ForEach-Object {$username = $_$groups = $\_ | Select-Object -Expand memberof |ForEach-Object { (Get-ADGroup $\_).Name }  
“{0}: {1}” -f $username, ($groups -join ', ')  
} | Out-File ‘C:\GS\Get Group List\Access Groups.csv’

Many Thanks

Gary

---

<div class="post-metadata">

**Author:** ![mortensummer](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@mortensummer](https://forums.powershell.org/u/mortensummer)\
**Post date:** [December 14, 2018, 5:02am UTC](https://forums.powershell.org/t/export-users-member-of-to-excel/11793/2 "2018-12-14T05:02:39Z")

</div>

Hi Gary,

How about this as a starter for 10?

```
$userlist = Get-Content 'C:\GS\Get Group List\User.txt'

foreach ($user in $userlist){

```
$Groups = (Get-ADPrincipalGroupMembership $user).Name

$GroupsCSV = $Groups| ForEach-Object {$_ + ','}

"&#8175;"$user&#8175;",&#8175;"$GroupsCSV&#8175;"" | out-file export.csv -append       

```

}
```
  
Not fully tested… so may need some tweaking…

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [December 14, 2018, 7:08am UTC](https://forums.powershell.org/t/export-users-member-of-to-excel/11793/3 "2018-12-14T07:08:48Z")

</div>

you can combine the foreach statement body to a oneliner.

```
foreach($User in $UserList){
 Get-ADPrincipalGroupMembership -Identity $User | Select-Object @{E={$_.Name};L='GroupName'},@{E={$User};L='UserName'} | Export-Csv -Path c:\GroupList.csv -NoTypeInformation -Append
}
```

---

<div class="post-metadata">

**Author:** ![mortensummer](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@mortensummer](https://forums.powershell.org/u/mortensummer)\
**Post date:** [December 14, 2018, 8:19am UTC](https://forums.powershell.org/t/export-users-member-of-to-excel/11793/4 "2018-12-14T08:19:50Z")

</div>

I ask this purely as a learning point for me…

but is there any performance gains doing it in the one-liner you have written, as opposed to the multi-line version from me?

Just for clarification I wrote it the way I did as its how my brain works… write one line \> build on it \> next line/function etc.

I suppose this is the beauty of programming\scripting…there is always more than one way to complete a specific task.

Ta!

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [December 14, 2018, 9:47am UTC](https://forums.powershell.org/t/export-users-member-of-to-excel/11793/5 "2018-12-14T09:47:19Z")

</div>

@kvprasoon: Your implementation would overwrite the CSV for every loop iteration.

@Tom\_Mort\_Yates: My most common answer to everything, it depends. Guessing from how you are building your code, you learned in another language like vbScript or something as the code isn’t “Powershell’y”. Part of the power of Powershell is the ability to pipe objects to other cmdlets, so you’re not leveraging this functionality when you break things out into separate lines.

The best way that I’ve seen and used to build object is like so (shout out to @Dave Wyatt):

```
...
#Assign variable to loop, any returns roll up to this variable
$results = foreach($User in $UserList){
     Get-ADPrincipalGroupMembership -Identity $User | 
         Select-Object @{E={$_.Name};L='GroupName'},@{E={$User};L='UserName'}
}
#Pipe all of the results to the CSV
$results | Export-Csv -Path c:\GroupList.csv -NoTypeInformation
```

---

<div class="post-metadata">

**Author:** ![mortensummer](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@mortensummer](https://forums.powershell.org/u/mortensummer)\
**Post date:** [December 14, 2018, 11:02am UTC](https://forums.powershell.org/t/export-users-member-of-to-excel/11793/6 "2018-12-14T11:02:41Z")

</div>

Thanks Rob - really useful. Absolutely spot on with where my coding came from, amongst other languages!

Will try and do more pipelining where appropriate. Love the example with the implementation of custom expressions.

Learning so much here 🙂

&nbsp;

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [December 14, 2018, 1:26pm UTC](https://forums.powershell.org/t/export-users-member-of-to-excel/11793/7 "2018-12-14T13:26:38Z")

</div>

Good catch, I missed -Append

---

<div class="post-metadata">

**Author:** ![ggman](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@ggman](https://forums.powershell.org/u/ggman)\
**Post date:** [December 15, 2018, 8:03pm UTC](https://forums.powershell.org/t/export-users-member-of-to-excel/11793/8 "2018-12-15T20:03:14Z")

</div>

Thank you so much, that worked.

---

<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:33pm UTC](https://forums.powershell.org/t/export-users-member-of-to-excel/11793/9 "2024-05-16T20:33:32Z")

</div>


