Defrag Method of the Win32_Volume Class

The Defrag method defragments the volume.

This topic uses Managed Object Format (MOF) syntax. For more information about using this method, see Calling a Method. The Defrag method, like the Format method, can run for a very long time. These methods are normally called asynchronously. If your code cancels the call by calling IWbemServices::CancelAsyncCall (or SWbemSink.Cancel in script or Visual Basic), then the provider is not notified and the operation continues to completion. During this time, no other disk defragmentation operations can be performed, because only one instance of a defragmentation engine can run at a time.

Syntax

MOF
 uint32 Defrag(
  [in]    boolean Force,
  [out]   object DefragAnalysis
);

Parameters

Force [in]

If true, forces defragmenting of the volume even if free space is low. The default is false.

DefragAnalysis [out]

A Win32_DefragAnalysis object that contains the properties that describe the extent to which the volume is fragmented.

Return Value

Returns one of the following numeric values.

Return codeDescription
0

Success

1

Access Denied

2

Not Supported

3

Volume Dirty Bit Is Set

4

Not Enough Free Space

5

Corrupt Master File Table Detected

6

Call Canceled

7

Call Cancellation Request Too Late

8

Defrag Engine Is Already Running

9

Unable To Connect To Defrag Engine

10

Defrag Engine Error

11

Unknown Error

 

Examples

For script code examples, see WMI Tasks for Scripts and Applications and the TechNet ScriptCenter Script Repository.

For C++ code examples, see WMI C++ Application Examples.

Requirements

Minimum supported clientNone supported
Minimum supported serverWindows Server 2003
MOFVds.mof
DLLVdswmi.dll
Namespace\root\cimv2

See Also

Win32_Volume

Send comments about this topic to Microsoft

Build date: 11/3/2009

Tags :


Community Content

Thomas Lee
Win32_Volume Class Defrag Method - Sample in PowerShell
# wmi-defragvolume.ps1
# Script to defrag the local C: volume using Powershell and WMI
# Thomas Lee - tfl@psp.co.uk
# First - get the volumes via WMI
$v=gwmi win32_volume
  
# Display Number of volumes
"Number of volumes {0}: " -f $v.length
# Now get the C:\ volume
$v1=$v | where {$_.name -eq "C:\"}
# Perform a defrag analysis
$v1.defraganalysis().defraganalysis
# Defrag the volume
$v1.defrag($true)
# Redo the Defrag analysis
$v1.defraganalysis().defraganalysis

Running this on my system returns the following output:

  
    
      
        
          
            
              
                
                  
                    
                      
PS C:\Users\Administrator> .\wmi-defragvolume.ps1

Number of volumes2:

__GENUS : 1
__CLASS : Win32_DefragAnalysis
__SUPERCLASS :
__DYNASTY : Win32_DefragAnalysis
__RELPATH : Win32_DefragAnalysis
__PROPERTY_COUNT : 27
__DERIVATION : {}
__SERVER : LHS1
__NAMESPACE : ROOT\CIMV2
__PATH : \\LHS1\ROOT\CIMV2:Win32_DefragAnalysis
AverageFileSize : 198959
AverageFragmentsPerFile : 1.05
AverageFreeSpacePerExtent : 54900541
ClusterSize : 4096
ExcessFolderFragments : 0
FilePercentFragmentation : 1
FragmentedFolders : 1
FreeSpace : 60061192192
FreeSpacePercent : 87
FreeSpacePercentFragmentation : 0
LargestFreeSpaceExtent : 34030051328
MFTPercentInUse : 91
MFTRecordCount : 45348
PageFileSize : 0
TotalExcessFragments : 1931
TotalFiles : 37038
TotalFolders : 8223
TotalFragmentedFiles : 10
TotalFreeSpaceExtents : 1094
TotalMFTFragments : 2
TotalMFTSize : 50790400
TotalPageFileFragments : 0
TotalPercentFragmentation : 0
TotalUnmovableFiles : 24
UsedSpace : 8655134720
VolumeName :
VolumeSize : 68716326912

__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 2
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
DefragAnalysis : System.Management.ManagementBaseObject
ReturnValue : 0

__GENUS : 1
__CLASS : Win32_DefragAnalysis
__SUPERCLASS :
__DYNASTY : Win32_DefragAnalysis
__RELPATH : Win32_DefragAnalysis
__PROPERTY_COUNT : 27
__DERIVATION : {}
__SERVER : LHS1
__NAMESPACE : ROOT\CIMV2
__PATH : \\LHS1\ROOT\CIMV2:Win32_DefragAnalysis
AverageFileSize : 198970
AverageFragmentsPerFile : 1
AverageFreeSpacePerExtent : 46885460
ClusterSize : 4096
ExcessFolderFragments : 0
FilePercentFragmentation : 0
FragmentedFolders : 1
FreeSpace : 60060274688
FreeSpacePercent : 87
FreeSpacePercentFragmentation : 1
LargestFreeSpaceExtent : 34251751424
MFTPercentInUse : 91
MFTRecordCount : 45353
PageFileSize : 0
TotalExcessFragments : 35
TotalFiles : 37038
TotalFolders : 8223
TotalFragmentedFiles : 1
TotalFreeSpaceExtents : 1281
TotalMFTFragments : 2
TotalMFTSize : 50790400
TotalPageFileFragments : 0
TotalPercentFragmentation : 0
TotalUnmovableFiles : 24
UsedSpace : 8656052224
VolumeName :
VolumeSize : 68716326912


PS C:\Users\Administrator>


Running this script on a Longhorn B3 VM produced the following output:

jhildeman
Any explanation for return code 10?
Can you list reasons for Return Value 10? I am running the "defragger.vbs" script listed and it runs perfect except the call to objVol.Defrag() always returns error code 10. I cannot find any details on what might cause this circumstance. I am running on a test machine with very little load/traffic.
Tags :

jhildeman
return code 10 - less than 20% free space on drive
Looks like you get this message on drives with less than 20% free space. I thought the analysis check would prevent that but apparently not.
Tags :

Page view tracker