Glowie
Glowie

Reputation: 2309

Simplify Powershell Where-Object

I have working Import-Csv statement that uses Where-Object

Import-Csv D:\Script\my.csv | Where-Object {$_.SourceIP -Like '10.251.22.11*' -Or $_.SourceIP -Like '10.251.22.*' -Or $_.DestinationIP -Like '10.251.22.11*' -Or $_.DestinationIP -Like '10.251.22.*'}

If I try to simplify the statement, it doesn't work

Import-Csv D:\Script\my.csv | Where-Object {($_.SourceIP -Like ('10.251.22.11*' -Or '10.251.22.*')) -Or ($_.DestinationIP -like ('10.251.22.11*' -Or '10.251.22.*'))}

Google is not helping :-(

Upvotes: 1

Views: 206

Answers (1)

Samuel Prout
Samuel Prout

Reputation: 665

Instead of -like, use -match in this case.

Import-Csv D:\Script\my.csv | Where-Object {$_.SourceIP -match '^10\.251\.22\.*' -or $_.DestinationIP -match '^10\.251\.22\.*'}

Also, 10.251.22.* will match 10.251.22.11*, so you can combine them.

Upvotes: 1

Related Questions