# howto populate "member Of" for a user account in AD

**URL:** https://forums.powershell.org/t/howto-populate-member-of-for-a-user-account-in-ad/13908
**Category:** PowerShell Help
**Created:** [March 4, 2020, 12:21am UTC](https://forums.powershell.org/t/howto-populate-member-of-for-a-user-account-in-ad/13908 "2020-03-04T00:21:53Z")
**Posts on this page:** 9
**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: [March 4, 2020, 12:21am UTC](https://forums.powershell.org/t/howto-populate-member-of-for-a-user-account-in-ad/13908/1 "2020-03-04T00:21:53Z")

</div>

Hi

as part of disabling an account I store the groups a user is member of in the notes section  
[pre]

#get all the groups this user is member of an paste this in Note section  
$groups =Get-ADPrincipalGroupMembership $user.SamAccountName  
Set-ADUser $user.samAccountName -Replace @{info=$groups.name -join “`r`n”}

[/pre]  
if for some reason a user has been wrongly disabled (in case of a contractor where the contract is extended at the last moment) how can I populate the member of again with all the groups stored in the Note section?

&nbsp;

&nbsp;

---

<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: [March 4, 2020, 1:41am UTC](https://forums.powershell.org/t/howto-populate-member-of-for-a-user-account-in-ad/13908/2 "2020-03-04T01:41:15Z")

</div>

I’d recommend to _join_ your list elements with something unusual what helps later to split them correctly into separate pieces again. … like

```
Set-ADUser $user.samAccountName -Replace @{info=$groups.name -join ‘_#_#\_’}
```
  
Now it’s easy to read the notes section and separate the single groups to use it to add the user to the groups.

---

<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: [March 4, 2020, 2:46am UTC](https://forums.powershell.org/t/howto-populate-member-of-for-a-user-account-in-ad/13908/3 "2020-03-04T02:46:03Z")

</div>

Olaf,

thanks for that, any thoughts on how to populate the Member of based upon the groups stored in the notes section?

Best regards

Paul

&nbsp;

---

<div class="post-metadata">

### Author: ![arikc77](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@arikc77](https://forums.powershell.org/u/arikc77)
#### Post date: [March 4, 2020, 4:27am UTC](https://forums.powershell.org/t/howto-populate-member-of-for-a-user-account-in-ad/13908/4 "2020-03-04T04:27:56Z")

</div>

If I understand correctly and you want to add the groups in $groups to the user , then you can try

foreach ($g in $groups) {

Get-ADGroup $g | Add-ADGroupMember -Members ( (Get-ADUser $user).DistinguishedName)

}

&nbsp;

### or with Try catch :
foreach ($g in $groups) {

Try { Get-ADGroup $g | Add-ADGroupMember -Members ( (Get-ADUser $user).DistinguishedName) }  
Catch {Write-Host “user $user wasn’t added to group $g”}

}

---

<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: [March 4, 2020, 6:14am UTC](https://forums.powershell.org/t/howto-populate-member-of-for-a-user-account-in-ad/13908/5 "2020-03-04T06:14:33Z")

</div>

Hi Arik,

not quite

in the above mentioned piece of code I store all the groups in the Note section of the user account.

when for some reason HR made a mistake and this user needs to be enabled again we need to add the groups he was member off before we disabled his account.  
the challenge is  
to grab the groups stored in the notes section and add these back into the member off tab  
the above mentioned code is only here for illustration on how I’ve exported the groups to the notes section of this user account

&nbsp;

thanks for your assistance

---

<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: [March 4, 2020, 4:08pm UTC](https://forums.powershell.org/t/howto-populate-member-of-for-a-user-account-in-ad/13908/6 "2020-03-04T16:08:55Z")

</div>

Hmmm … actually I don’t understand what’s the challange on that. You managed to **get** the **group memberships** of the user with **Get-ADPrincipalGroupMembership**. Then you used this to **set** the info for the **AD user** with **Set-ADUser**. Now you need to **get** the info from the **AD user** … so you use **Get-ADUser**. Now you use this info to **add** the **group memberships** of the user with **Add-ADPrincipalGroupMembership**.  
To accomplish this you need to **split** the group names again you **joined** before.

```
$groups =Get-ADPrincipalGroupMembership $user.SamAccountName
Set-ADUser $user.samAccountName -Replace @{info = $groups.sAMAccountName -join '_#_#_'}

$CurrState = Get-ADUser -Identity $user.SamAccountName -Properties Info 
$SavedADGroupList = $CurrState.Info -split '_#_#_'
Add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $SavedADGroupList
```

I’d recommend to use the sAMAccountName instead of the name of the groups because they are unique while the names might be not.

You may have to deal with the Primary Group of the users accounts as you cannot remove it from the accounts but you will have them in the list you’ve got from the membership list.

---

<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: [March 4, 2020, 4:16pm UTC](https://forums.powershell.org/t/howto-populate-member-of-for-a-user-account-in-ad/13908/7 "2020-03-04T16:16:28Z")

</div>

> [@](#):
>
> … or with Try catch …

Could you please use the code tags “ **PRE** ” to format your code as code?

Thank you.

---

<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: [March 5, 2020, 12:31am UTC](https://forums.powershell.org/t/howto-populate-member-of-for-a-user-account-in-ad/13908/8 "2020-03-05T00:31:36Z")

</div>

thanks Olaf that works like a charm 🙂

---

<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:30pm UTC](https://forums.powershell.org/t/howto-populate-member-of-for-a-user-account-in-ad/13908/9 "2024-05-16T20:30:24Z")

</div>


