20

I have lines -

echo $LocalAdmins
    Administrator
    Domain-Administrator
    daemon
    SomeUser
  • Line 1-3 should be same although there could be situation where Domain-Administrator doesn't exist so simply counting lines won't help because User might hide in 3rd line.

  • Line 4 string changes, there could be more than 4 lines either as well as no Line 4 at all.

  • I'm not sure yet if lines change their position or not.

I need to get string that is -notMatch "Administrator", "Domain-Administrator", "daemon". I.e., I need user names that are in this list.

Is there a way to use more than one -notMatch with -and or ()? Currently I'm stuck with code that use only one -notMatch. And I can't use -Match because there could be 2+ users in the list.

$LocalAdmins | Select-String -pattern "Administrator" -notMatch

2 Answers 2

27

Like this?

$LocalAdmins | select-string -Pattern 'Administrator|daemon' -NotMatch | select -expa line

-pattern accepts REGEX. You can use the | ( or regex operator ) to add others words to fit your needs.

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

Comments

2

Assuming that $LocalAdmins is an array you could do this:

$exclude = 'Administrator', 'Domain-Administrator', 'daemon'
$LocalAdmins | Where-Object { $exclude -notcontains $_ }

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.