PowerShell: Why does it take 3 seconds to access a recycled bin item?

104 views Asked by At

EDIT: Because of helpful comments, I'm revising this question, combining the two sample scripts given before into one.

The following PowerShell code accesses the first ten items in the recycle bin and outputs their original filenames and paths in a CSV file. It works, but it reports an elapsed time of about 3 to 4 seconds each time the code asks for the object of an item in the bin. It also reports a similar amount of time to count all the items in the bin. So it seems as if the delay may be caused just by the code accessing the bin object, and once the object is open, it can do whatever it wants in negligible time. But I can't speed this up by piping because I need a separate object for each bin item as an input parameter for the GetDetailsOf() method. (Am I wrong about that? Is there a way to pipe this process?)

Other people have reported in the comments that they've run this code (earlier version in two segments before this edit) with access of each bin item taking negligible time. So the problem is not the code, but something in my computer when the code accesses a bin object.

$oShell = New-Object -com shell.application
$ssfBitBucket = 10
$oRecycleBin = $oShell.Namespace($ssfBitBucket)
$aasBin = New-Object System.Collections.ArrayList

$dTimeAllStart = $(get-date)

echo ""
$dTimeCountBinStart = $(get-date)
echo ("Count of recycle bin objects = " + $oRecycleBin.Items().count)
echo ("Elapsed time on count of bin: " + ($(get-date) - $dTimeCountBinStart) + ".")

for ($nCount = 0 ; $nCount -lt 10 ; $nCount++)
   {$dTimeAccessOneItemStart = $(get-date)
    $oRBItem = $oRecycleBin.Items().Item($nCount)
    echo ("Elapsed time on item " + $nCount + ": " + ($(get-date) - $dTimeAccessOneItemStart) + ".")
    $asBinItem = New-Object PsObject -property `
        @{'Name'             = $oRecycleBin.GetDetailsOf($oRBItem, 0)
          'OriginalLocation' = $oRecycleBin.GetDetailsOf($oRBItem, 1)}
    $aasBin.add($asBinItem)
    echo $oRBItem.Name
    }

$aasBin `
    | select Name, OriginalLocation `
    | Export-Csv -Path $sOutputFilespec -NoTypeInformation

out-file -InputObject ("") -FilePath $sOutputFilespec -append

$dTimeAllEnd = $(get-date)
$dTimeAllElapsed = $dTimeAllEnd - $dTimeAllStart 
$sTimeAllElapsed = $dTimeAllElapsed.ToString("hh\:mm\:ss")
echo ("Elapsed time: " + $sTimeAllElapsed + ".")

This code is stripped-down from a script I want to run on the entire recycle bin. With 17,000 items, at 3 seconds per item, that's going to take 14 hours, which is way too long.

An additional wrinkle is that if I've been doing something else and come back to this, or if I restart the computer and run this with nothing else loaded, then the first access takes about 7 seconds. If the piece of code that counts the bin is there, then that takes 7 about seconds and everything else takes about 3 to 4. If the bin is not counted, then accessing the first item takes about 7 seconds and rest take about 3 to 4. But with or without counting the bin, if I run the code shortly after it's just run, then everything takes 3 to 4 seconds.

As reported in response to the comments:

  • Task Manager does not show any bump in memory usage when running the code.
  • From running my larger script, I know that the ten items range in size from 15 kb to 5.5 Mb. All are local on one SSD. All but one had their original location in one folder. The deletion dates range from April 2021 to Nov. 2022. In one particular run, the access times range from 3.0 to 3.8 seconds.
  • While NTFS can have performance problems with very large folders, those problems appear to occur with file counts over 100k, whereas my recycle bin has 17,189 items.

Edit 2: To check if my Internet security software, SentinelOne, is causing the delay, I went offline, disabled the SentinelOne agent, then ran the script again. The numbers seemed to change somewhat, but not enough to make a big difference.

How can I find out what my computer is doing that's making it take several seconds to access an object of the recycled bin?

0

There are 0 answers