# Scripting Defender - How to get quarantined files?

**URL:** <https://forums.powershell.org/t/scripting-defender-how-to-get-quarantined-files/15272>\
**Category:** PowerShell Help\
**Created:** [November 23, 2020, 4:57am UTC](https://forums.powershell.org/t/scripting-defender-how-to-get-quarantined-files/15272 "2020-11-23T04:57:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fjollberg](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/fjollberg/32/41_2.png) [@fjollberg](https://forums.powershell.org/u/fjollberg)\
**Post date:** [November 23, 2020, 4:57am UTC](https://forums.powershell.org/t/scripting-defender-how-to-get-quarantined-files/15272/1 "2020-11-23T04:57:45Z")

</div>

Hi,

I’m trying to script Windows Defender to make a custom scan on a path and want to find out what actions it made.

So, the hypothesis is to run Start-MpScan, wait for the job to finish and then use Get-MpThreatDetection to find out what happened. But I don’t seem to be able to get all the actions taken as I would kind of expect. In particular, files that are quarantined don’t generally show up, and I don’t quite understand why.

The files _are_ shown in Windows Defender as quarantined, so it’s obviously possible to get the information, but it seems not to be easily accessible from PS.

Any thoughts? Would be most appreciated.

Regards,  
/Fredrik

&nbsp;

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [November 23, 2020, 5:33am UTC](https://forums.powershell.org/t/scripting-defender-how-to-get-quarantined-files/15272/2 "2020-11-23T05:33:59Z")

</div>

For windows 10, you may be able to get the info from several PS cmdlets. I have no way to test as my system is clean.

> **[Get-MpThreat (Defender)](https://docs.microsoft.com/en-us/powershell/module/defender/get-mpthreat?view=windowsserver2022-ps&viewFallbackFrom=win10-ps)**
>
> Use this topic to help manage Windows and Windows Server technologies with Windows PowerShell.

&nbsp;

---

<div class="post-metadata">

**Author:** ![fjollberg](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/fjollberg/32/41_2.png) [@fjollberg](https://forums.powershell.org/u/fjollberg)\
**Post date:** [November 23, 2020, 5:41am UTC](https://forums.powershell.org/t/scripting-defender-how-to-get-quarantined-files/15272/3 "2020-11-23T05:41:57Z")

</div>

Well, yes. But they don’t provide the information I’m looking for, there seem to be a bit more magic involved.

I can, at least occasionally, get some of the files acted on in that output, but far from all. Which seem odd.

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [November 23, 2020, 5:53am UTC](https://forums.powershell.org/t/scripting-defender-how-to-get-quarantined-files/15272/4 "2020-11-23T05:53:56Z")

</div>

Understood.

What do you find here:

C:\ProgramData\Microsoft\Windows Defender\Quarantine

&nbsp;

---

<div class="post-metadata">

**Author:** ![fjollberg](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/fjollberg/32/41_2.png) [@fjollberg](https://forums.powershell.org/u/fjollberg)\
**Post date:** [November 23, 2020, 6:32am UTC](https://forums.powershell.org/t/scripting-defender-how-to-get-quarantined-files/15272/5 "2020-11-23T06:32:32Z")

</div>

Yes, as I wrote in the original post.

---

<div class="post-metadata">

**Author:** ![fjollberg](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/fjollberg/32/41_2.png) [@fjollberg](https://forums.powershell.org/u/fjollberg)\
**Post date:** [November 23, 2020, 6:49am UTC](https://forums.powershell.org/t/scripting-defender-how-to-get-quarantined-files/15272/6 "2020-11-23T06:49:51Z")

</div>

To be somewhat more specific. I’m using test-data from the Github repository [git@github.com](mailto:git@github.com):mattias-ohlsson/eicar-standard-antivirus-test-files.git.

When I use Start-MpScan to do a scan of the folder, 14 files are quarantined and 5 modified to remove harmful macros.

Get-MpThreatDetection will only give me an entry for one of the files, or in one case two, acted on. The others are silently quarantined or modified as far as the PS cmdlets are concerned, but visible in the Windows Defender GUI.

I would like to be able to compile a list of all the files, preferably without having to traverse all of the folders in the path before and after.

---

<div class="post-metadata">

**Author:** ![fjollberg](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/fjollberg/32/41_2.png) [@fjollberg](https://forums.powershell.org/u/fjollberg)\
**Post date:** [November 23, 2020, 11:13pm UTC](https://forums.powershell.org/t/scripting-defender-how-to-get-quarantined-files/15272/7 "2020-11-23T23:13:37Z")

</div>

The best source I’ve found so far is the event log. A bit cumbersome and doesn’t catch all quarantined files, but at least most of them.

```
$events = Get-WinEvent -LogName 'Microsoft-Windows-Windows Defender/Operational' -FilterXPath "*[System[EventID=1116]]" | Where-Object {$_.TimeCreated -ge $start_time}
$infected_files = @()

foreach ($event in $events) {
    $xml = [xml]$event.toXml()
    $threat = $xml.Event.EventData.Data | Where {$_.Name -eq 'Threat Name'} | Select -ExpandProperty "#text"
    $resources = $xml.Event.EventData.Data | Where {$_.Name -eq 'Path'} | Select -ExpandProperty "#text"
    foreach ($resource in ($resources -split ';')) {
        if ($resource.Contains($path)) {
            $resource = $resource.Trim()
            $resource = $resource -replace "^(file|containerfile):_", ""
            $resource = $resource -replace "^$([regex]::Escape($path))", ""
            $infected_files += [PSCustomObject]@{Threat = $threat; Resource = $resource}
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/scripting-defender-how-to-get-quarantined-files/15272/8 "2024-05-16T20:28:17Z")

</div>

Closed on May 16, 2024, 8:28pm UTC


