PowerShell – Exclude text on Select-String

PowerShell – Exclude text on Select-String

From the reference of Select-String, parameter -Exclude:

Exclude the specified items. The value of this parameter qualifies the
Path parameter
. Enter a path element or pattern, such as *.txt.
Wildcards are permitted.

So this is not what we need here.

As noted in the comments, you should filter $AVList instead:

# Get all AVs, excluding Windows Defender
$filteredAvList = @($AVList.displayName) -notlike *Windows Defender*

if (Select-String -Path $workingdirectoryAVList.txt -Pattern $filteredAvList -SimpleMatch -Quiet){
    # TODO: Replace $avList by $filteredAvList 

When a comparison operator like -notlike is applied to a collection, it effectively filters the collection, returning only the elements that match the condition. To make sure that the operator is always applied to an array, enclose the LHS operand with the array sub-expression operator @(). Otherwise you would get a boolean result when the LHS is a single object.

PowerShell – Exclude text on Select-String

Related post:

Leave a Reply

Your email address will not be published. Required fields are marked *