# How to use where-object within foreach loop

**URL:** <https://forums.powershell.org/t/how-to-use-where-object-within-foreach-loop/17282>\
**Category:** PowerShell Help\
**Created:** [August 11, 2021, 10:44am UTC](https://forums.powershell.org/t/how-to-use-where-object-within-foreach-loop/17282 "2021-08-11T10:44:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![TomasCrha](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tomascrha/32/1592_2.png) [@TomasCrha](https://forums.powershell.org/u/TomasCrha)\
**Post date:** [August 11, 2021, 10:44am UTC](https://forums.powershell.org/t/how-to-use-where-object-within-foreach-loop/17282/1 "2021-08-11T10:44:21Z")

</div>

Hello,  
currently I m in a middle of script creation and I am stuck with a certain thing - i need to go through a set of objects with foreach loop, and within the loop to filter each item according to a certain property.  
Let me put it in example:  
if I populate a variable with 2 names:  
$variable1 = “tomas.crha”,“petr.kalis”  
foreach ($x in $variable1)  
{  
IF ($x -like “tomas\*”)  
{  
write-host “tomas is there”

}

ELSE  
{  
write-host “tomas is not there”  
}  
}

and in each case of the condition I need to list out the result to a different variable or csv output/whatever  
So there will be a variable containing “tomas.crha”  
And different variable containing “petr.kalis”

Anyone has an idea how to do this?

Thank you,  
Tomas

---

<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:** [August 11, 2021, 11:12am UTC](https://forums.powershell.org/t/how-to-use-where-object-within-foreach-loop/17282/2 "2021-08-11T11:12:30Z")

</div>

Tomas,  
Welcome to the forum. 👋🏽

> [@TomasCrha](#):
>
> Anyone has an idea how to do this?

Your explanation is a bit vague. Could you elaborate a little more detailed please. It may help when you’d be a little more specific about your task or share some sample input data and the desired output.

In general you can use if statements or - if it’s about proper objects - you could use

> **[Where-Object (Microsoft.PowerShell.Core) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/where-object?view=powershell-7.4&viewFallbackFrom=powershell-7.1)**
>
> The Where-Object cmdlet selects objects that have particular property values from the collection of objects that are passed to it. For example, you can use the Where-Object cmdlet to select files that were created after a certain date, events with a...

To export the results to a CSV file you can use

> **[Export-Csv (Microsoft.PowerShell.Utility) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-7.4&viewFallbackFrom=powershell-7.1)**
>
> The Export-CSV cmdlet creates a CSV file of the objects that you submit. Each object is a row that includes a character-separated list of the object's property values. You can use the Export-CSV cmdlet to create spreadsheets and share data with...

Regardless of that - when you post code, error messages or sample data, please format it as code using the _preformatted text_ button ( **\</\>** ).

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![TomasCrha](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tomascrha/32/1592_2.png) [@TomasCrha](https://forums.powershell.org/u/TomasCrha)\
**Post date:** [August 11, 2021, 11:21am UTC](https://forums.powershell.org/t/how-to-use-where-object-within-foreach-loop/17282/3 "2021-08-11T11:21:17Z")

</div>

Hello Olaf, thank you for your reply  
I will try to put into the example that I provided, hopefully it will be understandable:  
$variable1 = “tomas.crha”,“petr.kalis” (variable contains names and I need to pull names with tomas to $variable2, and the rest to e.g. $variable3)  
So:  
foreach ($x in $variable1)  
{  
IF ($x -like “tomas\*”)  
{  
write-host “tomas is there” AND THEN POPULATE $variable2 with results matching IF condition

}

ELSE  
{  
write-host “tomas is not there” AND THEN POPULATE $variable3 with results matching ELSE condition  
}  
}

so I will have $variable2 with ‘tomas.crha’ value  
and $variable3 with ‘petr.kalis’ value

the actual goal is a bit more complicated, i am trying to make a report of Sharepoiint Online site owners - but sometmes the owner is a group, and sometimes it is an actual account, so I need to distinguish between these 2 somehow, and separate them from one another

Many thanks for any hints,  
Tomas

---

<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:** [August 11, 2021, 12:34pm UTC](https://forums.powershell.org/t/how-to-use-where-object-within-foreach-loop/17282/4 "2021-08-11T12:34:09Z")

</div>

> [@TomasCrha](#):
>
> the actual goal is a bit more complicated, i am trying to make a report of Sharepoiint Online site owners - but sometmes the owner is a group, and sometimes it is an actual account, so I need to distinguish between these 2 somehow, and separate them from one another

That’s what I meant. In PowerShell we can do a lot of things differently to other languages because we can use the advantages of objects with properties and are not limitted to text / strings.

I don’t have experiences with SharePoint but when you query the owner I’m sure the returned objects have a property like object category or something like this and this way you can distinguish between users and groups.

_Translated_ to your example would it look something like this:

```auto
$UserList1 = 'tomas.crha', 'petr.kalis'

$UserList2 =
    $UserList1 |
        Where-Object {$_ -like 'tomas*'}
$UserList3 =
    $UserList1 |
        Where-Object { $_ -like 'petr*' }

```

---

<div class="post-metadata">

**Author:** ![TomasCrha](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tomascrha/32/1592_2.png) [@TomasCrha](https://forums.powershell.org/u/TomasCrha)\
**Post date:** [August 13, 2021, 7:33pm UTC](https://forums.powershell.org/t/how-to-use-where-object-within-foreach-loop/17282/5 "2021-08-13T19:33:37Z")

</div>

Many thanks Olaf, this seems to work, at some point:)  
I am testing it further, and if you will not mind, I will ask for more advice from your side in upcoming days  
Once again, thank you very much!  
Tomas

---

<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:22pm UTC](https://forums.powershell.org/t/how-to-use-where-object-within-foreach-loop/17282/6 "2024-05-16T20:22:00Z")

</div>


