# Add local groups to local user account PS script

**URL:** <https://forums.powershell.org/t/add-local-groups-to-local-user-account-ps-script/1687>\
**Category:** PowerShell Help\
**Created:** [June 24, 2013, 2:13am UTC](https://forums.powershell.org/t/add-local-groups-to-local-user-account-ps-script/1687 "2013-06-24T02:13:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![googleuser](https://avatars.discourse-cdn.com/v4/letter/g/e9a140/32.png) [@googleuser](https://forums.powershell.org/u/googleuser)\
**Post date:** [June 24, 2013, 2:13am UTC](https://forums.powershell.org/t/add-local-groups-to-local-user-account-ps-script/1687/1 "2013-06-24T02:13:52Z")

</div>

Hello, I am a PowerShell 3.0 novice and want to add a feature to my first ever production script tool.

The script below will get all local user accounts from remote computers listed inÂ a .txt file then export to .csv format.Â How exactly can I incorporate what local groups each local user account is a member of?Â For each local user account retrieved I want to list what local groups it is a member of.

get-ciminstance -classname win32\_useraccount -filter “localaccount=‘true’” -computername (get-content C:\PS&lt;listofcomputers\>.txt) | export-csv c:\PS&lt;name\>\_localuseraccounts.csv (export to .csv)

Your insight is greatly appreciated,

Matt, PS 3.0 novice

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [June 24, 2013, 7:47am UTC](https://forums.powershell.org/t/add-local-groups-to-local-user-account-ps-script/1687/2 "2013-06-24T07:47:17Z")

</div>

Group information is held in Win32\_Group. You can see the relationship between users and groups by dumping the Win32\_GroupUser instances. You will see a load of entries like this

GroupComponent : Win32\_Group (Name = “Administrators”, Domain = “RSLAPTOP01”)  
PartComponent : Win32\_UserAccount (Name = “Administrator”, Domain = “RSLAPTOP01”)  
PSComputerName :  
CimClass : root/cimv2:Win32\_GroupUser  
CimInstanceProperties : {GroupComponent, PartComponent}  
CimSystemProperties : Microsoft.Management.Infrastructure.CimSystemProperties

WMI classes have associations - in this case there is an association between the Win32\_User and the Win32\_Group classes. The Win32\_GroupUser can be thought of as the linking class. What we need to do to answer your question is to go from the individual instances of Win32\_User (the users) to the associated groups.  
Something like this should do it

```
$data = Get-CimInstance -ClassName Win32_UserAccount -Filter "LocalAccount = $true" |
foreach {
  $groups = Get-CimAssociatedInstance -InputObject $PSItem -ResultClassName Win32_Group | select -ExpandProperty Name
  #$PSItem.name
  #$groups
  $PSItem | Add-Member -MemberType NoteProperty -Name "Groups" -Value ($groups -join ";") -PassThru
} 
$data | select Caption, Groups
```

Alternatively pipe straight into you CSV

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [June 25, 2013, 5:47am UTC](https://forums.powershell.org/t/add-local-groups-to-local-user-account-ps-script/1687/3 "2013-06-25T05:47:21Z")

</div>

I’ve posted a script that does the inverse at  
[http://msmvps.com/blogs/richardsiddaway/archive/2013/06/25/the-inverse-association.aspx](http://msmvps.com/blogs/richardsiddaway/archive/2013/06/25/the-inverse-association.aspx)

---

<div class="post-metadata">

**Author:** ![googleuser](https://avatars.discourse-cdn.com/v4/letter/g/e9a140/32.png) [@googleuser](https://forums.powershell.org/u/googleuser)\
**Post date:** [June 25, 2013, 6:51am UTC](https://forums.powershell.org/t/add-local-groups-to-local-user-account-ps-script/1687/4 "2013-06-25T06:51:53Z")

</div>

Thanks Richard.Â I apologize in advance for stupid questions for I am a PowerShell (and scripting in general) novice.Â

I tried to copy & paste into PS3 ISE the script you provided and it does not work.Â What am I doing wrong?Â As you can see from attached picture of my ISE, when running the selection of get-ciminstance… it works.Â Maybe I am out of my depth in this forum.

Another Q: I notice you do it -filter “LocalAccount = $true”.Â Is there a difference between that and â€œlocalaccount=â€™trueâ€™â€ ?

&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:57pm UTC](https://forums.powershell.org/t/add-local-groups-to-local-user-account-ps-script/1687/5 "2024-05-16T20:57:53Z")

</div>

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


