# Foreach-object manipulation

**URL:** <https://forums.powershell.org/t/foreach-object-manipulation/24479>\
**Category:** PowerShell Help\
**Created:** [July 23, 2024, 6:27am UTC](https://forums.powershell.org/t/foreach-object-manipulation/24479 "2024-07-23T06:27:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Renatjon22](https://avatars.discourse-cdn.com/v4/letter/r/e495f1/32.png) [@Renatjon22](https://forums.powershell.org/u/Renatjon22)\
**Post date:** [July 23, 2024, 6:27am UTC](https://forums.powershell.org/t/foreach-object-manipulation/24479/1 "2024-07-23T06:27:38Z")

</div>

Hi.

May be someone could help me in my problemm 🙂

I have $userAD.Properties.memberof

PS C:\Users\renat\> $userAD.Properties.memberof  
CN=Support Team,OU=AD-Groups,OU=lala,DC=lala,DC=com  
CN=User Support Team,OU=AD-Groups,OU=lala,DC=lala,DC=com  
CN=Test,OU=ServiceAndGroups,DC=lala,DC=com  
CN=TestHR,OU=ServiceAndGroups,DC=lala,DC=com  
CN=TestPR,OU=ServiceAndGroups,DC=lala,DC=com

I need to create $adGroups where be presented cn names of the groups that start with Test

$adGroups = $userAD.Properties.memberof | ForEach-Object ??? | Where-Object {$\_.StartsWith(“Test”)}

the result should be like this:

PS C:\Users\renat\> $userGroups  
Test  
TestHR  
TestPR

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [July 23, 2024, 7:43am UTC](https://forums.powershell.org/t/foreach-object-manipulation/24479/2 "2024-07-23T07:43:00Z")

</div>

Hi, welcome to the forum 👋

Firstly, when posting code in the forum, please can you use the preformatted text \</\> button. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working). If you can’t see the \</\> in your toolbar, you will find it under the gear icon.

[How to format code on PowerShell.org](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif)

Filter the groups with `Where-Object`.  
Split the string for the remaining groups on ‘,OU=’ and select the first element in the array of split strings (element 0).  
Remove `CN=`

```PowerShell
$adGroups = $userAD.Properties.MemberOf | 
    Where-Object {$_ -like 'CN=Test*'} | 
        ForEach-Object {($_ -split ',OU=')[0] -replace 'CN='}

```

---

<div class="post-metadata">

**Author:** ![Renatjon22](https://avatars.discourse-cdn.com/v4/letter/r/e495f1/32.png) [@Renatjon22](https://forums.powershell.org/u/Renatjon22)\
**Post date:** [July 23, 2024, 8:57am UTC](https://forums.powershell.org/t/foreach-object-manipulation/24479/3 "2024-07-23T08:57:10Z")

</div>

matt-bloomfield/ thank you for the answer.

I will take into account your remark regarding formatting the text.

The script you provided works, thank you very much.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [August 22, 2024, 8:57am UTC](https://forums.powershell.org/t/foreach-object-manipulation/24479/4 "2024-08-22T08:57:20Z")

</div>

Closed on August 22, 2024, 8:57am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
