1

I try to make a PowerShell script to do the following:
I want to identify the antivirus running on my PC.

I use command to do that:

$AntiVirusProduct = Get-WmiObject -Namespace root\SecurityCenter2 -Class AntiVirusProduct 
Write-Output $AntiVirusProduct.DisplayName

Here I get the antivirus name but, I don't know how to grep the antivirus name and put it to the next command. The next command is:

Stop-Service -Force "$Antivirus Name"

Or if there is a better way to to this?

Edit

Get-WmiObject -Namespace root\SecurityCenter2 -Class AntiVirusProduct |
    Select DisplayName
DisplayName
-----------
AVG Antivirus
Windows Defender
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Skyn3t
  • 29
  • 4
  • `Stop-Service $AntiVirusProduct.DisplayName -Force`? Of course this will only work if the display name returned is actually the name of the service. – Ansgar Wiechers Jul 01 '18 at 14:02
  • It dosen't work, because the output is like this : Windows Defender AVG Antivirus and I don't know how to grep only AVG Antivirus without Windows Defender. Stop-Service : Cannot find any service with service name 'Windows Defender'. – Skyn3t Jul 01 '18 at 14:06
  • The service name doesn't neccessarily match the registered AntiVirusProduct name. Here it doesn't match with Avira. I'd try `Get-Service *AVG* | Stop-Service -Confirm` or whatever your antivirus is. Please [edit] your question to contain additional information. –  Jul 01 '18 at 14:16

1 Answers1

1

If your display name output is a list of service names you can remove the Windows Defender entry from the list with something like this:

$svc = $AntiVirusProduct.DisplayName |
       Where-Object { $_ -notlike '*Windows Defender*' }

and then stop the service like this:

$svc | Stop-Service -Force
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328