2

I am trying to discover if there has been a defrag run on our servers. To save myself going to each one and loading the defrag program and checking the "Last Run" column I thought WMI might have this info. I have been checking the Win32_Volume class and can see this can be used to initiate defrags and run the analysis. What I can't find is any reference to the last time it completed. I would rather just pull an object down than run an analysis each time. Is there a way to find this information through WMI or a reg key?

2 Answers 2

2

Run a query for Event ID 258 in the Application event log

PS F:\ps1> get-winevent -filterHashTable @{Logname = 'Application'; ID = '258';} | select-object -first 1 -prop machinename,timecreated,message | fl

MachineName : coyote.acme.com
TimeCreated : 9/24/2014 2:28:07 AM
Message     : The disk defragmenter successfully completed defragmentation on (C:)
2
  • Didn't think of that. Had to go with Get-EventLog in the end as the defrag messages will not display for Get-WinEvent which I can't fathom. My method is much slower but it has to run overnight which is no great hardship. At least then I will have something! Probably have a followup question about the blank messages object soon lol
    – user35213
    Sep 25, 2014 at 16:17
  • 1
    IIRC get-eventlog came with an earlier version of PS and can query events from remote targets of any OS including older OS's like 2003. Get-WinEvent came with a later PS, operates more quickly/efficiently, but requires the remote target be 2008 (R2?) or later.
    – Clayton
    Sep 25, 2014 at 23:36
0

I see you already have an answer to this. But you might look at the question a little differently if you wanted to use the DefragAnalysis.

$driveLetter = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter = 'C:'"
$driveDefrag = $driveLetter.DefragAnalysis()
$driveDefrag.DefragAnalysis

$report.DefragRecommended will display if the OS thinks it needs a defrag and $report.DefragAnalysis has a lot of additional useful information as well.

You must log in to answer this question.