# Get-ADPrincipalGroupMembership and save it to .csv report

**URL:** <https://forums.powershell.org/t/get-adprincipalgroupmembership-and-save-it-to-csv-report/21062>\
**Category:** PowerShell Help\
**Created:** [December 1, 2022, 6:18pm UTC](https://forums.powershell.org/t/get-adprincipalgroupmembership-and-save-it-to-csv-report/21062 "2022-12-01T18:18:03Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [December 1, 2022, 6:18pm UTC](https://forums.powershell.org/t/get-adprincipalgroupmembership-and-save-it-to-csv-report/21062/1 "2022-12-01T18:18:03Z")

</div>

Hi all,  
I am looking for help, this is what i try to acomplishe  
I am loking for disabled users and moving them to Disabled OU, then before i clean up this OU, i want to get and save all data about this users with GroupMembership etc…  
this is my code and in some reason it saving just one user,

Please, help me to fix this  
Thanks

```auto
$Users = Get-ADUser -Filter {LastLogonTimeStamp -lt $time -and Enabled -eq $false} -SearchBase $sb -ResultPageSize 0 -Property Name, LastLogonDate, DistinguishedName, SamAccountName

ForEach ($user in $Users)
{
 $UserGroups = (Get-ADUser $user -Properties memberof | select -expand memberof | get-adgroup) | select Name,groupscope

 $UserProperties = [pscustomobject][ordered] @{
                    "Name" = $user.name
                    "DistinguishedName" = $user.DistinguishedName
                    "User ID" = $User.samaccountname
                    "Email Address" = $User.emailaddress
                    "Title" = $User.title
                    "Description" = $User.Description
                    "Department" = $User.Department
                    #"Manager" = $managerDetails.Name
                    "Office Phone" = $User.officephone
                    "Mobile" = $User.mobile
                    "Account Created" = $User.Created
                    "LastLogonDate" = $user.LastLogonDate
                    "Password Last Changed" = $User.PasswordLastSet
                    "AD Group Information" = $UserGroups.Name -join ';'
                    
                }
}

$UserProperties | Export-Csv -Path "$Report" -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:** [December 1, 2022, 6:28pm UTC](https://forums.powershell.org/t/get-adprincipalgroupmembership-and-save-it-to-csv-report/21062/2 "2022-12-01T18:28:55Z")

</div>

Hi welcome back to the forum. You are setting the $userproperties variable inside your foreach loop. Remove the assignment inside and put it in front of the foreach.

`$userproperties = foreach(…)`

---

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [December 1, 2022, 7:13pm UTC](https://forums.powershell.org/t/get-adprincipalgroupmembership-and-save-it-to-csv-report/21062/3 "2022-12-01T19:13:28Z")

</div>

> [@krzydoug](#):
>
> $userproperties =

Thx krzdoug,  
i try your way, but didnt worked, file empty

```auto
$UserProperties = ForEach ($user in $Users)
{

 $UserGroups = (Get-ADUser $user -Properties memberof | select -expand memberof | get-adgroup) | select Name,groupscope

 [pscustomobject][ordered] @{
                    "Name" = $user.name
                    "DistinguishedName" = $user.DistinguishedName
                    "User ID" = $User.samaccountname
                    "Email Address" = $User.emailaddress
                    "Title" = $User.title
                    "Description" = $User.Description
                    "Department" = $User.Department
                    #"Manager" = $managerDetails.Name
                    "Office Phone" = $User.officephone
                    "Mobile" = $User.mobile
                    "Account Created" = $User.Created
                    "LastLogonDate" = $user.LastLogonDate
                    "Password Last Changed" = $User.PasswordLastSet
                    "AD Group Information" = $UserGroups.Name -join ';'
                    
                }
}

$UserProperties | Export-Csv -Path "$Report" -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:** [December 1, 2022, 7:24pm UTC](https://forums.powershell.org/t/get-adprincipalgroupmembership-and-save-it-to-csv-report/21062/4 "2022-12-01T19:24:10Z")

</div>

If you’re not getting output then the variable will be empty. Remove the variable and the export and just confirm you’re getting the output you expect.

---

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [December 1, 2022, 8:01pm UTC](https://forums.powershell.org/t/get-adprincipalgroupmembership-and-save-it-to-csv-report/21062/5 "2022-12-01T20:01:47Z")

</div>

you right , $UserProperties empty  
now my question why?  
if i run $UserGroups, i received all data for one user

---

<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:** [December 1, 2022, 10:23pm UTC](https://forums.powershell.org/t/get-adprincipalgroupmembership-and-save-it-to-csv-report/21062/6 "2022-12-01T22:23:23Z")

</div>

Not with the code shown. The code show $usergroups would have two properties: name and groupscope. Perhaps you’re running modified version of what is shown?

---

<div class="post-metadata">

**Author:** ![posh\_enjoyer](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@posh\_enjoyer](https://forums.powershell.org/u/posh_enjoyer)\
**Post date:** [December 2, 2022, 2:54pm UTC](https://forums.powershell.org/t/get-adprincipalgroupmembership-and-save-it-to-csv-report/21062/7 "2022-12-02T14:54:20Z")

</div>

For me, it was confusing to call the ForEach loop = $UserProperties because I don’t know if that is per user collection or the whole, so made a slight adjustment.  
I believe in this example its ok to have $UserProperties inside the loop.

```auto
$Users = Get-ADUser -Filter {LastLogonTimeStamp -lt $time -and Enabled -eq $false} -SearchBase $sb -ResultPageSize 0 -Properties *

$Results = ForEach ($user in $Users) {

    $UserGroups = $User | Select-Object -ExpandProperty memberof | Get-ADGroup | Select-Object Name,groupscope 

    [pscustomobject][ordered] @{
        "Name" = $$User.name
        "DistinguishedName" = $$User.DistinguishedName
        "User ID" = $$User.samaccountname
        "Email Address" = $$User.emailaddress
        "Title" = $$User.title
        "Description" = $$User.Description
        "Department" = $$User.Department
        #"Manager" = $managerDetails.Name
        "Office Phone" = $$User.officephone
        "Mobile" = $$User.mobile
        "Account Created" = $$User.Created
        "LastLogonDate" = $$User.LastLogonDate
        "Password Last Changed" = $User.PasswordLastSet
        "AD Group Information" = $UserGroups.Name -join ';'
        }
}
$Results | Export-Csv -Path "$Report" -NoTypeInformation

```

edit: Updated to include first line which was initially missed, removed Get-ADUser from inside loop

---

<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:** [December 2, 2022, 2:59pm UTC](https://forums.powershell.org/t/get-adprincipalgroupmembership-and-save-it-to-csv-report/21062/8 "2022-12-02T14:59:50Z")

</div>

> [@posh\_enjoyer](#):
>
> I believe in this example its ok to have $UserProperties inside the loop.

… but you need to query each user twice … I’d consider this as inefficient. 😉

---

<div class="post-metadata">

**Author:** ![posh\_enjoyer](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@posh\_enjoyer](https://forums.powershell.org/u/posh_enjoyer)\
**Post date:** [December 2, 2022, 3:41pm UTC](https://forums.powershell.org/t/get-adprincipalgroupmembership-and-save-it-to-csv-report/21062/9 "2022-12-02T15:41:58Z")

</div>

I did not see original poster mention speed as an issue 😉  
You are 100% correct but I do not understand Regex at all to parse out the MemberOf property to a nicely formatted GroupName;GroupName; 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:** [December 2, 2022, 4:09pm UTC](https://forums.powershell.org/t/get-adprincipalgroupmembership-and-save-it-to-csv-report/21062/10 "2022-12-02T16:09:04Z")

</div>

> [@posh\_enjoyer](#):
>
> I did not see original poster mention speed as an issue

Until a certain degree that should be mandatory. 😉

> [@posh\_enjoyer](#):
>
> You are 100% correct but I do not understand Regex at all to parse out the MemberOf property to a nicely formatted GroupName;GroupName; etc

What? 🤔 … you don’t need regex at all … what I meant was - you query all users once in advance to get the list of users you want to iterate over and then you query each individual user again inside the loop to get their properties … that’s redundand.

---

<div class="post-metadata">

**Author:** ![posh\_enjoyer](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@posh\_enjoyer](https://forums.powershell.org/u/posh_enjoyer)\
**Post date:** [December 2, 2022, 4:22pm UTC](https://forums.powershell.org/t/get-adprincipalgroupmembership-and-save-it-to-csv-report/21062/11 "2022-12-02T16:22:46Z")

</div>

I initially missed the top line of code and was unaware the format of input. Also, just noticed the button that copies ALL code and will do that going forward. 😉  
Edit incoming!

---

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [December 2, 2022, 8:56pm UTC](https://forums.powershell.org/t/get-adprincipalgroupmembership-and-save-it-to-csv-report/21062/12 "2022-12-02T20:56:57Z")

</div>

I want to Thank you all to participate in this conversation, but in some reason this still come up empty in report

```auto
"AD Group Information" = $UserGroups.Name -join ';'

```

---

<div class="post-metadata">

**Author:** ![posh\_enjoyer](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@posh\_enjoyer](https://forums.powershell.org/u/posh_enjoyer)\
**Post date:** [December 2, 2022, 9:28pm UTC](https://forums.powershell.org/t/get-adprincipalgroupmembership-and-save-it-to-csv-report/21062/13 "2022-12-02T21:28:08Z")

</div>

Glad to help, apologies for the multiple edits. I see what happened, forgot to rename a variable in my changes.

I made another edit and corrected this part from:

```auto
 $UserGroups = $UserProperties | Select-Object -ExpandProperty memberof | Get-ADGroup | Select-Object Name,groupscope 

```

To:

```auto
 $UserGroups = $User | Select-Object -ExpandProperty memberof | Get-ADGroup | Select-Object Name,groupscope 

```

---

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [December 5, 2022, 4:05pm UTC](https://forums.powershell.org/t/get-adprincipalgroupmembership-and-save-it-to-csv-report/21062/14 "2022-12-05T16:05:22Z")

</div>

Thx posh\_enjoyer for your help  
all looking good now

---

<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:16pm UTC](https://forums.powershell.org/t/get-adprincipalgroupmembership-and-save-it-to-csv-report/21062/15 "2024-05-16T20:16:19Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


