Users of Hyper-V virtual machines with virtual hard disk files need to consider how much storage these files take up. VHD and VHDX files can grow unwieldy over time, as new content expands their file size. However, deleting content from a VHD does not shrink its file size. This means that virtual machine users are wasting storage and costs with blank data. This is especially problematic in thin provisioning setups. Don’t fret, though; there is a way to compact VHD and VHDX files to reclaim that missing space. It is possible to do this with a Compact function and three commands in PowerShell. Here’s how to compact VHD and VHDX files in Hyper-V 2012.

First, be sure to shut down the VM with the VHD/VHDX to be compacted. Open up PowerShell as an Administrator by pressing WinKey+X, and then choose PowerShell (Run as Administrator). Accept the User Account Control prompt either by clicking Yes or pressing Alt+Y.
In PowerShell, run the commands below, in this order, to mount the VHD locally. Next optimize its file size and then dismount the virtual hard disk file.
Mount-VHD -Path "C:ClusterStorageVolume1VM1Virtual Hard DisksVM1.vhdx" -ReadOnly
Optimize-VHD -Path "C:ClusterStorageVolume1VM1Virtual Hard DisksVM1.vhdx" -Mode Full
Dismount-VHD "C:ClusterStorageVolume1VM1Virtual Hard DisksVM1.vhdx"
Be sure to replace the file names in the commands with the VHD or VHDX file you are looking to optimize. Users can maximize the free space they reclaim by defragmenting the hard disk and emptying the Recycle Bin. Third-party tools that “zero out” deleted data can reclaim even more space.
While Hyper-V’s Edit Virtual Hard Disk Wizard offers a Compact disk option, this will not actually shrink the VHD or VHDX in use. Unless the user mounts the virtual hard disk locally beforehand, running the Compact command will have no effect.
These steps will ensure that dynamic VHD and VHDX files will not use up excess storage and save on costs with running virtual machines. Have more virtualization questions? Contact our team of Sikich experts to help.
This publication contains general information only and Sikich is not, by means of this publication, rendering accounting, business, financial, investment, legal, tax, or any other professional advice or services. This publication is not a substitute for such professional advice or services, nor should you use it as a basis for any decision, action or omission that may affect you or your business. Before making any decision, taking any action or omitting an action that may affect you or your business, you should consult a qualified professional advisor. In addition, this publication may contain certain content generated by an artificial intelligence (AI) language model. You acknowledge that Sikich shall not be responsible for any loss sustained by you or any person who relies on this publication.
About the Author
John Branch
John has over 30 years of experience working with technology. His career began in the US Navy where he contributed to several communities including Naval Aviation, Naval Special Warfare, and Naval Cryptography. Upon transitioning to the private sector, he delivered technical training on foundational networking technologies including switching and routing as a Certified Cisco Systems Instructor (CSSI). For over 15 years, John has been working as a network consultant assisting clients in achieving their business objectives through technology and trusted advice. John’s operational expertise includes complex network design and implementation (routing, switching, wireless, and security) with Cisco, Meraki, Juniper, and HPE Aruba networking equipment. John has considerable experience with server virtualization using Microsoft Hyper-V and Azure. He also has actively worked in cybersecurity developing assessment and testing methodologies for his clients. He holds several certifications from industry leaders including Cisco, Microsoft, Citrix, and SonicWall.
Sign up for Insights
Join 14,000+ Business executives and decision makers.
Latest Insights
Information Technology
IT Horror Stories: Ignoring the Warning Signs
November 4, 2025
ERP
The Cost of Standing Still with Legacy ERP
November 3, 2025
Cloud
Is Cloud Storage Safe for Law Firms?
October 31, 2025
Life Science
The Haunting of Finance Teams: 5 Frights Life Sciences Leade...
October 30, 2025
Technology
Addressing Conflict During the Lifecycle of Project Engageme...
October 29, 2025
Article
What Is AP Automation & Why It Matters
October 28, 2025
Information Technology
SSL Certificate File Formats Explained
October 24, 2025
Dynamics 365>Dynamics 365 Finance and Supply Chain Management
Introducing The Sikich HEADSTART Finance and Supply Chain Su...
October 23, 2025
Life Science
Archive vs. Migrate: Key Considerations for Managing QMS Dat...
October 22, 2025
Information Technology
IT Horror Stories: The Lack of IT Maturity
October 21, 2025