Wednesday, July 16, 2014

#18 : How to find Disk Fragmentation with Powershell

Hi Guys,

While doing a research on other topic, I came to know that there is a way to find the disk fragmentation details with Powershell. Below lines of code are sufficient to find disk fragmentation.

$drive = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter = 'e:'"
$report = $drive.DefragAnalysis() > $null
$report.DefragAnalysis


Output: 
__GENUS : 1
__CLASS : Win32_DefragAnalysis
__SUPERCLASS :
__DYNASTY : Win32_DefragAnalysis
__RELPATH : Win32_DefragAnalysis
__PROPERTY_COUNT : 27
__DERIVATION : {}
__SERVER : DIVINE
__NAMESPACE : ROOT\CIMV2
__PATH : \\DIVINE\ROOT\CIMV2:Win32_DefragAnalysis
AverageFileSize : 72
AverageFragmentsPerFile : 1
AverageFreeSpacePerExtent : 8503984128
ClusterSize : 4096
ExcessFolderFragments : 1
FilePercentFragmentation : 0
FragmentedFolders : 1
FreeSpace : 42684555264
FreeSpacePercent : 81
FreeSpacePercentFragmentation : 45
LargestFreeSpaceExtent : 23402860544
MFTPercentInUse : 100
MFTRecordCount : 44159
PageFileSize : 0
TotalExcessFragments : 1
TotalFiles : 32684
TotalFolders : 5792
TotalFragmentedFiles : 1
TotalFreeSpaceExtents : 5
TotalMFTFragments : 2
TotalMFTSize : 45219840
TotalPageFileFragments : 0
TotalPercentFragmentation : 0
TotalUnmovableFiles : 10
UsedSpace : 9744240640
VolumeName :
VolumeSize : 52428795904

Line#2 might take some longer time depending on the disk size. So, if you are planning to run scan on all the servers of your environment - it might take longer time. But, the results are extremely accurate.

Happy scripting!!

No comments:

Post a Comment

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...