How to use where-object within foreach loop

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

Tomas,
Welcome to the forum. :wave:t4:

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

To export the results to a CSV file you can use

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.

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

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:

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

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

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