2

I have a custom object with the Properties: Name, Value and others.

I'm trying to filter using Where by the name property, using an array:

$NamesToExclude= @('Person1', 'Person2', 'Person3', 'Person4')

$CustomObj | Where-Object {$_.Name -ne $NamesToExclude}

This returns all excluded names (and others). I have also tried:

{$_.Name -ne $($NamesToExclude)}

and tried using the -notlike operator. What am I doing wrong?

2 Answers 2

3

You have an [Array] but are using it like a [String]

Instead of -notlike or -ne, use -notin:

$CustomObj | Where-Object {$_.Name -notin $NamesToExclude}

about_Operators goes into more detail.

Sign up to request clarification or add additional context in comments.

Comments

0

Have a go with -notin!

$CustomObj | Where-Object {$_.Name -notin $NamesToExclude}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.