# About
Drive fragmentation is where files on a hard disk drive (HDD) or solid-state drive (SSD) are not stored in contiguous blocks but rather scattered in various non-contiguous locations on the storage media.
# Fragmentation Status
PowerShell commands to analyze a volume
```
Optimize-Volume -DriveLetter H -Analyze -Verbose
```
# Defrag Command in Windows
* A volume must have at least 15% free space for defrag to completely and adequately defragment it. defrag uses this space as a sorting area for file fragments. If a volume has less than 15% free space, defrag will only partially defragment it.
## Parameters
| Parameter | Description |
| --- | --- |
|\<volume> | Specifies the drive letter or mount point path of the volume to be defragmented or analyzed.|
| /a | Perform analysis on the specified volumes.|
|/c |Perform the operation on all volumes.|
|/d |Perform traditional defrag (this is the default). On a tiered volume though, traditional defrag is performed only on the Capacity tier.|
|/e| Perform the operation on all volumes except those specified.|
|/g |Optimize the storage tiers on the specified volumes.|
|/h |Run the operation at normal priority (default is low).|
|/i \[n] |Tier optimization would run for at most n seconds on each volume.
|/k |Perform slab consolidation on the specified volumes.
|/l |Perform retrim on the specified volumes.|
|/m \[n]| Run the operation on each volume in parallel in the background. At most n threads optimize the storage tiers in parallel.
|/o| Perform the proper optimization for each media type.
|/t |Track an operation already in progress on the specified volume.
|/u |Print the progress of the operation on the screen.
|/v |Print verbose output containing the fragmentation statistics.
|/x |Perform free space consolidation on the specified volumes.
|/? |Displays this help information.
## Examples
To defragment the volume on drive C while providing progress and verbose output, type:
```bash
defrag c: /u /v
```
# Fragmentation in Virtual Environments
If the virtual disk is fixed, then there is little chance of the virtual disk binary file becoming fragmented on the [[Storage Area Network (SAN)]] - however, within the virtual disk file the operating system behaves the same way and will create fragmentation within the virtual disk itself.
The fragmentation within the virtual disk is caused by the OS running in the VM. The fragmentation at the storage level is generally caused by having many dynamic disks on the same physical volume.
My thinking is that if you have fragmentation inside of the VHD, then you have a third tier of fragmentation - inside (VM OS), outside (LUN file system), and physical (SAN RAID configuration). And it would seem to me that as you add more fragmentation you increase spindle seeks. However, as the sheer speed of spindles and heads increases - the actual performance gain from defragmentation is diminished. So there is the technical way of looking at this as well - do you gain performance?
Most storage folks would say that there is really no gain in performance and thus it is a waste of time. Most old time admins say to do it. My personal preference is middle between the two.
## Resolving Fragmentation in Virtual Environments
```powershell
mount-vhd "D:\VMNAME\Virtual Hard Disks\DISKNAME.vhdx"
defrag e: /h /x
defrag e: /h /k /l
defrag e: /h /x
defrag e: /h /k
dismount-vhd "D:\VMNAME\Virtual Hard Disks\DISKNAME.vhdx"
optimize-vhd "D:\VMNAME\Virtual Hard Disks\DISKNAME.vhdx" -Mode Full
```
# Sources
https://social.technet.microsoft.com/wiki/contents/articles/738.hyper-v-defragmentation.aspx
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/defrag