Where -notmatch

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.
User avatar
localpct
Posts: 221
Joined: Wed Sep 09, 2015 12:53 pm

Where -notmatch

Post by localpct » Thu May 18, 2017 8:51 am

So I'm still working through an application to exclude apps ( viewtopic.php?f=18&t=11296&start=10 )

I'm having a problem that when my code reads

#$_.Name -notmatch "1E Agent", it includes an application called Autonomy Qfiniti 3.5 SP2U3

Any thoughts?

Code: Select all

$script:files = Invoke-Command -ScriptBlock { Get-ChildItem \\$FQDN\C$\Windows\logs -File } |
Select-Object Name | where {

#region #
$_.Name -notmatch "$($textbox1.Text)" -and
$_.Name -notmatch ".dpx" -and
$_.Name -notmatch ".LOG2" -and
$_.Name -notmatch ".Net Data" -and
$_.Name -notmatch ".Net Framework" -and
$_.Name -notmatch ".txt" -and
$_.Name -notmatch "_32bit" -and
$_.Name -notmatch "_64bit" -and
$_.Name -notmatch "_Installation" -and
$_.Name -notmatch "_Installer" -and
$_.Name -notmatch "_PoSh" -and
$_.Name -notmatch "_SuccessCheck" -and
$_.Name -notmatch "_Update" -and
#$_.Name -notmatch "E Agent" -and
$_.Name -notmatch "810F.50" -and
$_.Name -notmatch "9470" -and
$_.Name -notmatch "6.1.0.373-x64" -and
$_.Name -notmatch "6.1.0.373-x86"

User avatar
jvierra
Posts: 11270
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Where -notmatch

Post by jvierra » Thu May 18, 2017 9:30 am

The correct way to do this is like this:

$_.Name -notmatch ($textbox1.Text + '|\.dpx|\.LOG2|\.Net Data ... etc ...')

All special characters must be escaped.

User avatar
jvierra
Posts: 11270
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Where -notmatch

Post by jvierra » Thu May 18, 2017 9:34 am

"match" any is OR and "match all" is AND.

User avatar
localpct
Posts: 221
Joined: Wed Sep 09, 2015 12:53 pm

Re: Where -notmatch

Post by localpct » Thu May 18, 2017 12:25 pm

jvierra wrote:The correct way to do this is like this:

$_.Name -notmatch ($textbox1.Text + '|\.dpx|\.LOG2|\.Net Data ... etc ...')

All special characters must be escaped.
Maybe I'm missing something, but does it have to be written on one line?

I've tried
$_.Name -notmatch ($textbox1.Text + '|\.dpx
|\.LOG2|
\.Net Data')

and

$_.Name -notmatch ($textbox1.Text + '|\.dpx|
\.LOG2|
\.Net Data')

And it stops filtering

User avatar
jvierra
Posts: 11270
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Where -notmatch

Post by jvierra » Thu May 18, 2017 12:29 pm

$_.Name -notmatch ($textbox1.Text + '|\.dpx|\.LOG2|\.Net Data')

You cannot have line breaks in regex.

User avatar
localpct
Posts: 221
Joined: Wed Sep 09, 2015 12:53 pm

Re: Where -notmatch

Post by localpct » Thu May 18, 2017 1:02 pm

jvierra wrote:$_.Name -notmatch ($textbox1.Text + '|\.dpx|\.LOG2|\.Net Data')

You cannot have line breaks in regex.
Okay, I did all of that but now when I add Adobe Shockwave Player it removes Autonomy Qfiniti 3.5 SP2U3

Something in the line must be doubled, or maybe an additional space somewhere... Not really sure

This list has 411 applications to exclude :roll:

User avatar
jvierra
Posts: 11270
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Where -notmatch

Post by jvierra » Thu May 18, 2017 1:13 pm

"match" will match any part of the name. You must test each pattern. Without the contents of the textbox it is hard to say. Nothing in Adobe matches the pattern.

Anything that does not match the patterns will be passed.

$_.Name -notmatch ($textbox1.Text + '|\.dpx|\.LOG2|\.Net Data|Qfiniti ')

Code: Select all

PS>'Autonomy Qfiniti 3.5 SP2U3' -match '\.dpx|\.LOG2|\.Net Data|Qfinity'
False
PS>'Autonomy Qfiniti 3.5 SP2U3' -match '\.dpx|\.LOG2|\.Net Data|Qfiniti'
True
PS>'Autonomy Qfiniti 3.5 SP2U3' -notmatch '\.dpx|\.LOG2|\.Net Data|Qfiniti'
False
PS>'Autonomy Qfiniti 3.5 SP2U3' -notmatch '\.dpx|\.LOG2|\.Net Data'
True
PS>
You have to figure out which logic you want to use.

User avatar
localpct
Posts: 221
Joined: Wed Sep 09, 2015 12:53 pm

Re: Where -notmatch

Post by localpct » Fri May 19, 2017 10:31 am

I need it to -notmatch against 400+ applications

But I re did my where {$_.Name } line and everything seems to be pulling correctly.

Thanks!

User avatar
jvierra
Posts: 11270
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Where -notmatch

Post by jvierra » Fri May 19, 2017 10:41 am

Create an array of app names and use "-notin"

$appnames = Get-Content appnames.txt

$name -notin $appnames

User avatar
jvierra
Posts: 11270
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Where -notmatch

Post by jvierra » Fri May 19, 2017 10:55 am

Here is a very efficient way to do this:

Code: Select all

$apps = '.Net', '.DOC', 'Something'
#$apps = Get-Content appslist.txt
$nametocheck = 'Microsoft.Net'
$apps |
ForEach-Object -Begin { $found = $false } -Process {
if ($found) { break }
$found = if ($nametocheck -match [regex]::Escape($_)) { $true }
}
$found
#

Locked