# ActiveDirectory find all groups a user is member off

**URL:** <https://forums.powershell.org/t/activedirectory-find-all-groups-a-user-is-member-off/12847>\
**Category:** PowerShell Help\
**Created:** [July 11, 2019, 5:25am UTC](https://forums.powershell.org/t/activedirectory-find-all-groups-a-user-is-member-off/12847 "2019-07-11T05:25:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![acer4605](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@acer4605](https://forums.powershell.org/u/acer4605)\
**Post date:** [July 11, 2019, 5:25am UTC](https://forums.powershell.org/t/activedirectory-find-all-groups-a-user-is-member-off/12847/1 "2019-07-11T05:25:37Z")

</div>

Hi

I can succesfully export a list from a given adgroup to csv

```
Get-AdGroupMember -identity "Office365" | select samaccountname |export-csv -path c:\output\office_teams_sam.csv -NoTypeInformation
```

now I want to use this list to search through AD to find other groups where they are member of

only I don’t know how this is done can you help me out please

&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:** [July 11, 2019, 6:08am UTC](https://forums.powershell.org/t/activedirectory-find-all-groups-a-user-is-member-off/12847/2 "2019-07-11T06:08:03Z")

</div>

almost there,

You can iterate through each element in CSV and do a Get-ADUser on each name with -Properties memberof

```
# below code inside Foreach 
Get-ADUser $CurrentUser.samaccountname -Properties memberof | Export-CSv -Path <csv_path> -NoTypeInformation
```

---

<div class="post-metadata">

**Author:** ![acer4605](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@acer4605](https://forums.powershell.org/u/acer4605)\
**Post date:** [July 11, 2019, 6:37am UTC](https://forums.powershell.org/t/activedirectory-find-all-groups-a-user-is-member-off/12847/3 "2019-07-11T06:37:18Z")

</div>

Hi I still get an error here

```
$OfficeTeamsUser = Import-Csv -Path C:\output\office_teams_sam.csv
foreach ($CurrentUser in $OfficeTeamsUser){
Get-ADUser $CurrentUser.SamAccountName -Properties memberof |Export-csv -path C:\Temp\output.csv -NoTypeInformation
}
```

when I use samaccountname I get this error  
Get-ADUser : Cannot validate argument on parameter ‘Identity’. The argument is null. Provide a valid value for the argument, and then try running the command again.  
At line:3 char:12

- Get-ADUser $CurrentUser.SamAccountName -Properties memberof |Export-c …
- 

```auto

```

- CategoryInfo : InvalidData: (🙂 [Get-ADUser], ParameterBindingValidationException
- FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser

when I use $CurrentUser.name I get only 1 entry in my Csv

&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:** [July 11, 2019, 7:15am UTC](https://forums.powershell.org/t/activedirectory-find-all-groups-a-user-is-member-off/12847/4 "2019-07-11T07:15:14Z")

</div>

make sure there is a value in $CurrentUser.SamAccountname

---

<div class="post-metadata">

**Author:** ![acer4605](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@acer4605](https://forums.powershell.org/u/acer4605)\
**Post date:** [July 11, 2019, 7:33am UTC](https://forums.powershell.org/t/activedirectory-find-all-groups-a-user-is-member-off/12847/5 "2019-07-11T07:33:52Z")

</div>

I’ve got 1 column in my CSV with header and from row 2 all the users  
“SamAccountName”  
“dally”

&nbsp;

by preference I would like to search through 5 different office groups (group 1 to 5) search for all the users in there. these need to be exported to a csv file with

1colum with the username and the second with all the groups he’s member of example

“dally”,“Office365”, “Office365\_Admins”

thanks for your help

---

<div class="post-metadata">

**Author:** ![shawnwat](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@shawnwat](https://forums.powershell.org/u/shawnwat)\
**Post date:** [July 11, 2019, 10:18am UTC](https://forums.powershell.org/t/activedirectory-find-all-groups-a-user-is-member-off/12847/6 "2019-07-11T10:18:57Z")

</div>

I ran across this not too long ago. I don’t think this is the original post I used but should work:  
[https://techibee.com/active-directory/powershell-how-to-get-all-the-ad-groups-current-user-belongs/1672](https://techibee.com/active-directory/powershell-how-to-get-all-the-ad-groups-current-user-belongs/1672)

---

<div class="post-metadata">

**Author:** ![lake](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@lake](https://forums.powershell.org/u/lake)\
**Post date:** [July 12, 2019, 2:24am UTC](https://forums.powershell.org/t/activedirectory-find-all-groups-a-user-is-member-off/12847/7 "2019-07-12T02:24:28Z")

</div>

Hello,

Is did like this, is this ok?

This my first post, where I have included code, I hope this ok…

```
$result = @()
$OfficeTeamsUsers = Get-AdGroupMember -identity "Office365" | select samaccountname

foreach($user in $OfficeTeamsUsers) {
    $memberof = Get-ADUser $User.samaccountname -Properties memberof 

    $table = New-Object psobject
    $table | Add-Member -NotePropertyName "samaccountname" -NotePropertyValue $User.samaccountname
    $table | Add-Member -NotePropertyName "memberof" -NotePropertyValue $memberof

   $result += $table
   $table = $null
}

$result | Export-CSv -Path C:\Temp\output.csv -NoTypeInformation
```

---

<div class="post-metadata">

**Author:** ![lake](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@lake](https://forums.powershell.org/u/lake)\
**Post date:** [July 12, 2019, 2:25am UTC](https://forums.powershell.org/t/activedirectory-find-all-groups-a-user-is-member-off/12847/8 "2019-07-12T02:25:25Z")

</div>

Hello,

Is did like this, is this ok?

This my first post, where I have included code, I hope this ok…

```
$result = @()
$OfficeTeamsUsers = Get-AdGroupMember -identity "Office365" | select samaccountname

foreach($user in $OfficeTeamsUsers) {
    $memberof = Get-ADUser $User.samaccountname -Properties memberof 

    $table = New-Object psobject
    $table | Add-Member -NotePropertyName "samaccountname" -NotePropertyValue $User.samaccountname
    $table | Add-Member -NotePropertyName "memberof" -NotePropertyValue $memberof

    $result += $table
    $table = $null
}

$result | Export-CSv -Path C:\Temp\output.csv -NoTypeInformation
```

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [July 12, 2019, 7:28pm UTC](https://forums.powershell.org/t/activedirectory-find-all-groups-a-user-is-member-off/12847/9 "2019-07-12T19:28:04Z")

</div>

Don’t over think this. This is a very common daily thing. Really a PowerShell ADDS 101 thing.

There are already pre-built scripts on the MS [powershellgallery.com](http://powershellgallery.com) for this use case and more. AS well as all over the internet.

> https://www.powershellgallery.com/packages?q=%27group+member%27
> 
> See also:
> 
> Find Circular Nested Groups  
> PowerShell script to find any instances of Circular Nested Groups in the domain.
> 
> > **[Browse code samples](https://learn.microsoft.com/en-us/samples/browse/?redirectedfrom=TechNet-Gallery)**
> >
> > Get started with Microsoft developer tools and technologies. Explore our samples and discover the things you can build.
> 
> Get nested group membership - function  
> This function will recursively enumerate members of a given group along with nesting level and parent group information. If there is a circular membership, it will be displayed in Comment column.It accepts input from pipeline and works well with get-adgroup.  
> [Browse code samples | Microsoft Learn](https://gallery.technet.microsoft.com/scriptcenter/Get-nested-group-15f725f2)

  
It also could be as simple as this…

```
Get-ADUser userName –Properties MemberOf).MemberOf
```

Or even as simple as this…

```
Get-ADPrincipalGroupMembership username | select name
```

&nbsp;

If you really want to get elegant about this… then see this…

> http://vcloud-lab.com/entries/active-directory/powershell-active-directory-list-complete-hierarchy-of-upstream-nested-groups-recursively-of-user
> 
> > **[Powershell Active Directory: Show treeview of User or Group memberof hierarchy](http://vcloud-lab.com/entries/active-directory/powershell-active-directory-show-treeview-of-user-or-group-memberof-hierarchy)**
> >
> > After going through many testing and successfully streamlining most of the Users and Groups member of in active directory environment using Powershell Active Directory: List complete hierarchy of u...
> 
> [Powershell: List Active Directory group hierarchy](https://social.technet.microsoft.com/Forums/office/en-US/1fda5252-bd0f-4615-8238-cf08537b741f/powershell-list-active-directory-group-hierarchy)

  
&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

---

<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:32pm UTC](https://forums.powershell.org/t/activedirectory-find-all-groups-a-user-is-member-off/12847/10 "2024-05-16T20:32:00Z")

</div>


