Search code examples
windowspowershellantivirus

Powershell: Find installed Antivirus, filtering out Windows Defender


all.

I have a PowerShell script which will detect antivirus software installed in Windows (it's actually fairly common). The problem is, I want it to filter Windows Defender, especially since Windows 8, 8.1, and 10 come with it installed. I want my script to indicate whether or not Windows Defender if the ONLY antivirus software present and give a different output if it is.

Here's what I have so far...

function Get-AntivirusName { 
[cmdletBinding()]     
param ( 
[string]$ComputerName = "$env:computername" , 
$Credential 
) 
    BEGIN  
        { 
            $wmiQuery = "SELECT * FROM AntiVirusProduct" 
        } 
    PROCESS  
        {    
            $AntivirusProduct = Get-WmiObject -Namespace "root\SecurityCenter2" -Query $wmiQuery  @psboundparameters         
            $AntivirusNames = $AntivirusProduct.displayName       
            if ($AntivirusNames -eq "") {
                Write-host "Anti-Virus is NOT installed!"
            } 
            elseif ($AntivirusNames -eq "Windows Defender") {
                Write-host "ONLY Windows Defender is installed!"
            }
            else {
                Write-host "Anti-Virus is installed (" + $AntivirusNames + ")."
            }

        } 
     END { 
         } 
}

Get-AntivirusName 

The result is, no matter which other antivirus software apps are installed, it keeps telling me only Windows Defender is installed. Can someone point out what I'm missing?

Thanks so much in advance!


Solution

  • My guess is it's checking each element of the array in the elseif statement so is true even if there are multiple results. You could add a condition that there is just one result as follows:

    elseif ($AntivirusNames -eq "Windows Defender" -and $AntivirusNames.count -eq 1) {
                    Write-host "ONLY Windows Defender is installed!"
                }