Use -notlike to filter out multiple strings in PowerShell

后端 未结 9 513
执念已碎
执念已碎 2020-12-14 15:58

I\'m trying to read the event log for a security audit for all users except two, but is it possible to do that with the -notlike operator?

It\'s somethi

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 16:22

    V2 at least contains the -username parameter that takes a string[], and supports globbing.

    V1 you want to expand your test like so:

    Get-EventLog Security | ?{$_.UserName -notlike "user1" -and $_.UserName -notlike "*user2"}
    

    Or you could use "-notcontains" on the inline array but this would only work if you can do exact matching on the usernames.

    ... | ?{@("user1","user2") -notcontains $_.username}

提交回复
热议问题