0

I have a hashtable ($applications) that looks like that:

Name                                                        Version
----                                                        -------
Adobe Flash Player 19 ActiveX                               19.0.0.226
Enterprise Library 4.0 - May 2008                           4.0.0.0
Entity Framework Designer for Visual Studio 2012 - enu      11.1.21009.00
Fiddler                                                     2.4.0.6
Google Chrome                                               49.0.2623.87
IIS 8.0 Express                                             8.0.1557

So I'm trying to exclude some applications from the list and for that I'm using:

[array]$excludeApps = "Fiddler","Chrome"
foreach ($excludeApp in $excludeApps){
    $applications = $applications -notlike "*$excludeApp*"
}

The result maybe filtered out the exclude list but as not as I expected:

@{Name=Adobe Flash Player 19 ActiveX; Version=19.0.0.226}
@{Name=Enterprise Library 4.0 - May 2008; Version=4.0.0.0}
@{Name=Entity Framework Designer for Visual Studio 2012 - enu; Version=11.1.21009.00}
@{Name=IIS 8.0 Express; Version=8.0.1557}

I tried to handle those values with GetEnumerator() and several syntax of ${applications.Name} but nothing is worked. I believe that PowerShell detect this list as string.

Any idea how to handle this?

0

2 Answers 2

1

Use select-object with -match/-notmatch Operator.

i.e.:

$application = $application | ? { $_.name -notmatch "fiddler" -or $_.name -notmatch "Chrome"}

or in the Loop:

[array]$excludeApps = "Fiddler","Chrome"
foreach ($excludeApp in $excludeApps){
    $applications = $applications | ? {$_.name -notmatch $excludeApp }
}
Sign up to request clarification or add additional context in comments.

Comments

0

For one thing, your data is not a hashtable, but a list of custom objects, probably from importing a CSV. If you want to filter those objects by a list of partial matches (Fiddler, Chrome) on one of the properties (Name) the best approach is to build a regular expression:

$excludeApps = 'Fiddler', 'Chrome'
$expr = @($excludeApps | ForEach-Object { [regex]::Escape($_) }) -join '|'

and then filter the list with a -notmatch condition in a Where-Object statement:

$applications | Where-Object { $_.Name -notmatch $expr }

2 Comments

This solution will exclude any application that includes those words like "Multi Chrome Converter", " Fiddler on the Roof", etc.
Which is what the OP was asking.

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.