******************************************* If the VHDX is stored on SSD drives ******************************************* $sizeBefore = (Get-Item ).length Mount-VHD -Path 'vhdx folderpath\vhdxname.vhdx' defrag x: /x /h /u defrag x: /k /l /h /u defrag x: /x /h /u defrag x: /k /h /u Dismount-VHD Optimize-VHD -Mode Full $sizeAfter = (Get-Item ).length Write-Host "Total disk space Saved: $(($sizeBefore - $sizeAfter) /1Mb) MB" -Foreground Yellow ******************************************* If the VHDX is stored on Hard Drives (/x /k /l are retained for Trim capable SANs) ******************************************* $sizeBefore = (Get-Item ).length Mount-VHD -Path 'vhdx folderpath\vhdxname.vhdx' defrag x: /d /h /u defrag x: /x /h /u defrag x: /k /l /h /u defrag x: /x /h /u defrag x: /k /l /h /u Dismount-VHD Optimize-VHD -Mode Full $sizeAfter = (Get-Item ).length Write-Host "Total disk space Saved: $(($sizeBefore - $sizeAfter) /1Mb) MB" -Foreground Yellow ******************************************* Mount-VHD -Path 'vhdx folderpath\vhdxname.vhdx' -ReadOnly Optimize-VHD -Path 'vhdx folderpath\vhdxname.vhdx' -Mode Full Dismount-VHD 'vhdx folderpath\vhdxname.vhdx' ******************************************* https://www.c-amie.co.uk/technical/how-to-compact-vhdx-files-in-the-most-efficient-way/ https://www.ubackup.com/enterprise-backup/compact-vhdx.html https://www.itprotoday.com/windows-78/q-how-can-i-monitor-status-command-line-defrag-windows-7-and-above https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/defrag Get more info about the defrag: /V Consolidate free space on a drive: /X Defrag at normal process priority: /H Track a defrag already in progress: /T Defrag all drives except the one listed: /E Defrag all drives at the same time: /M – note, this is only advisable if you have multiple drives, and will not work good on a computer with multiple partitions