Where -notmatch

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Where -notmatch

Post by localpct »

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?
  1. $script:files = Invoke-Command -ScriptBlock { Get-ChildItem \\$FQDN\C$\Windows\logs -File } |
  2.     Select-Object Name | where {
  3.                
  4.         #region #
  5.         $_.Name -notmatch "$($textbox1.Text)" -and
  6.         $_.Name -notmatch ".dpx" -and
  7.         $_.Name -notmatch ".LOG2" -and
  8.         $_.Name -notmatch ".Net Data" -and
  9.         $_.Name -notmatch ".Net Framework" -and
  10.         $_.Name -notmatch ".txt" -and
  11.         $_.Name -notmatch "_32bit" -and
  12.         $_.Name -notmatch "_64bit" -and
  13.         $_.Name -notmatch "_Installation" -and
  14.         $_.Name -notmatch "_Installer" -and
  15.         $_.Name -notmatch "_PoSh" -and
  16.         $_.Name -notmatch "_SuccessCheck" -and
  17.         $_.Name -notmatch "_Update" -and
  18.         #$_.Name -notmatch "E Agent" -and
  19.         $_.Name -notmatch "810F.50" -and
  20.         $_.Name -notmatch "9470" -and
  21.         $_.Name -notmatch "6.1.0.373-x64" -and
  22.         $_.Name -notmatch "6.1.0.373-x86"
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Where -notmatch

Post by jvierra »

The correct way to do this is like this:

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

All special characters must be escaped.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Where -notmatch

Post by jvierra »

"match" any is OR and "match all" is AND.
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Where -notmatch

Post by localpct »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Where -notmatch

Post by jvierra »

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

You cannot have line breaks in regex.
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Where -notmatch

Post by localpct »

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:
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Where -notmatch

Post by jvierra »

"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 ')
  1. PS>'Autonomy Qfiniti 3.5 SP2U3'  -match '\.dpx|\.LOG2|\.Net Data|Qfinity'
  2. False
  3. PS>'Autonomy Qfiniti 3.5 SP2U3'  -match '\.dpx|\.LOG2|\.Net Data|Qfiniti'
  4. True
  5. PS>'Autonomy Qfiniti 3.5 SP2U3'  -notmatch '\.dpx|\.LOG2|\.Net Data|Qfiniti'
  6. False
  7. PS>'Autonomy Qfiniti 3.5 SP2U3'  -notmatch '\.dpx|\.LOG2|\.Net Data'
  8. True
  9. PS>
You have to figure out which logic you want to use.
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Where -notmatch

Post by localpct »

I need it to -notmatch against 400+ applications

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

Thanks!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Where -notmatch

Post by jvierra »

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

$appnames = Get-Content appnames.txt

$name -notin $appnames
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Where -notmatch

Post by jvierra »

Here is a very efficient way to do this:
  1. $apps = '.Net', '.DOC', 'Something'
  2. #$apps = Get-Content appslist.txt
  3. $nametocheck = 'Microsoft.Net'
  4. $apps |
  5.     ForEach-Object -Begin { $found = $false } -Process {
  6.         if ($found) { break }
  7.         $found = if ($nametocheck  -match [regex]::Escape($_)) { $true }
  8.     }
  9. $found
  10. #
This topic is 6 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked