# Pulling  user groups from a list of users

**URL:** <https://forums.powershell.org/t/pulling-user-groups-from-a-list-of-users/10823>\
**Category:** PowerShell Help\
**Created:** [June 15, 2018, 6:35pm UTC](https://forums.powershell.org/t/pulling-user-groups-from-a-list-of-users/10823 "2018-06-15T18:35:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikes94](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@mikes94](https://forums.powershell.org/u/mikes94)\
**Post date:** [June 15, 2018, 6:35pm UTC](https://forums.powershell.org/t/pulling-user-groups-from-a-list-of-users/10823/1 "2018-06-15T18:35:27Z")

</div>

File example(csv)

```
username
mike johnson
steve clarke
henry smith
```

Code so far:

```
Import-Csv C:\Users.csv | 
    ForEach-Object{
        Get-AdUser -Filter "displayname -eq '$($_.username)'" -Properties memberof
    } |
    Select-Object samaccountname, name, @{n='memberof';e={$_.memberof -join ';'}} |
    Export-Csv -Path C:\UserPermiss.csv -NoTypeInformation
```

This pulls back the correct info, but I want just the group name, not the whole CN=, OU= etc…

Cheers,  
Mike

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [June 15, 2018, 8:30pm UTC](https://forums.powershell.org/t/pulling-user-groups-from-a-list-of-users/10823/2 "2018-06-15T20:30:35Z")

</div>

The simplest way to get that is just to use regex to pull the values out:

```
Import-Csv -Path 'C:\Users.csv' | 
    Select-Object -ExpandProperty Username |
    Get-ADUser -Properties memberof |
    Select-Object -Property samaccountname, name, @{
        Name = 'memberof'
        Expression = {
            @($_.memberof) -match "(?<=CN\=)(.+)(?=,OU\=)" -join ';'
        }
    } | Export-Csv -Path C:\UserPermiss.csv -NoTypeInformation
```

In short, that goes through the array outputting everything that appears between “CN=” and “,OU=” in each string, then joins it with ‘;’ characters. Feel free to add spaces there or change that for another character or whatever you wanna do.

This is one of those interesting things where the -match operator operates on arrays a bit differently vs single items, so we’re ensuring it gets an array to look at.

---

<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:34pm UTC](https://forums.powershell.org/t/pulling-user-groups-from-a-list-of-users/10823/3 "2024-05-16T20:34:54Z")

</div>

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


