Skip to content

Remove-DefenderThreatRemote

Windows: Removes active threats detected by Windows Defender

#Requires -Version 5.1
#Requires -Modules Defender

[CmdletBinding()]
Param (
    [string]$ComputerName = $env:COMPUTERNAME,

    [pscredential]$Credential
)

Process {
    try {
        $session = $null
        if ($ComputerName -ne $env:COMPUTERNAME) {
            $sessionParams = @{
                'ComputerName' = $ComputerName
                'ErrorAction'  = 'Stop'
            }
            if ($null -ne $Credential) {
                $sessionParams.Add('Credential', $Credential)
            }
            $session = New-CimSession @sessionParams
            Remove-MpThreat -CimSession $session -ErrorAction Stop
        }
        else {
            Remove-MpThreat -ErrorAction Stop
        }

        $result = [PSCustomObject]@{
            ComputerName = $ComputerName
            Action       = "ActiveThreatsRemoved"
            Status       = "Success"
            Timestamp    = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }

        Write-Output $result
    }
    catch {
        throw
    }
    finally {
        if ($null -ne $session) {
            Remove-CimSession $session
        }
    }
}

Specifies the name of the target computer. Defaults to the local computer.

Specifies a PSCredential object for remote connection.

An interactive directory of PowerShell scripts.