1

I'm creating a PowerShell script to list down all the AV installed on a machine. I wanted to exclude the Windows Defender on the "If" statement when doing the checks. Below is my script:

   default
       {
       $result1 = 'There are {0} AV products installed on this system' -f $AVList.Count
       $result2 = 'DisplayNames = {0}' -f ($AVList.displayName -join ', ')
       if (Select-String -Path $workingdirectory\AVList.txt -Pattern $AVList.displayName -Exclude 'Windows Defender' -SimpleMatch -Quiet){
       $writeTxt4 = ("$(Get-Date) - [INFO]",'There are {0} AV products installed on this system.' -f $AVList.Count)
       $writeTxt5 = ("$(Get-Date) - [INFO]",'Anti-Virus Names = {0}' -f ($AVList.displayName -join ', '))
       Write-Output $writeTxt4 $writeTxt5
       Add-Content -path $report $writeTxt4
       Add-Content -path $report $writeTxt5
       }else{
       $writeTxt4 = ("$(Get-Date) - [INFO]",'There are {0} AV products installed on this system. Smile.' -f $AVList.Count)
       $writeTxt5 = ("$(Get-Date) - [INFO]",'Anti-Virus Names = {0}' -f ($AVList.displayName -join ', '))
       Write-Output $writeTxt4 $writeTxt5
         Add-Content -path $report $writeTxt4
       Add-Content -path $report $writeTxt5
       }

I tried to exclude it but still unable to make it work. Thank you so much for your help.

6
  • Please reduce your code to a minimal reproducible example of what you're trying to achieve Commented Feb 17, 2022 at 0:30
  • @SantiagoSquarzon I've now reduced it. basically, how can I exclude the Windows Defender on the select string test. On the AVList result, I got two AV showing up, BitDefender and Windows Defender. Commented Feb 17, 2022 at 0:45
  • if $AVList.displayName contains Windows Defender then what you are doing won't work. By the way, this is not a minimal reproducible example. Commented Feb 17, 2022 at 1:13
  • correct the $AVList.displayName contains Windows Defender. any idea how can I exclude it from the condition? Also, that's the main goal of my concern. thank you. Commented Feb 17, 2022 at 2:34
  • 1
    just filter $AVList.displayName to all elements where they don't match "Windows Defender" before passing it to Select-String Commented Feb 17, 2022 at 2:59

1 Answer 1

3

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 AV's, excluding "Windows Defender"
$filteredAvList = @($AVList.displayName) -notlike '*Windows Defender*'

if (Select-String -Path $workingdirectory\AVList.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.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks @zett42. I just did some adjustment and was able to make it work now. here's the updated script: $filteredAvList = @($AVList.displayName) -notlike '*Windows Defender*' if (Select-String -Path $workingdirectory\AVList.txt -Pattern $filteredAvList.displayName -SimpleMatch -Quiet){
@IamBayMax Thanks for the correction, I've updated my answer. The Select-String call is not right in your comment, it should be -Pattern $filteredAvList, because $filteredAvList is just an array of strings now.
i tried to use the -Pattern $filteredAvList and for some reason, not working, so I used $filteredAvList.displayName but yeah the good thing is that it is now doing what I want. thank you so much

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.