• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
OpenTechTips

OpenTechTips

Short Guides for IT Professionals

MENUMENU
  • HOME
  • ALL TOPICS
    • Active Directory
    • Exchange
    • InfoSec
    • Linux
    • Networking
    • Scripting
      • PowerShell
    • SSL
    • Virtualization
    • Web
    • Tools
  • ABOUT
  • SUBSCRIBE
Home » How to Compact a VHD or VHDX File using PowerShell

How to Compact a VHD or VHDX File using PowerShell

April 1, 2022 - by Zsolt Agoston - last edited on April 1, 2022

Compacting the virtual hard disk files can make their size significantly smaller, depending on how much white space they have.

The older (or more used) the drive, the better your chances are for massive space gain.

Compact VHD/VHDX

1. Open an elevated PowerShell window (Win + X keys, then press A).

How to Compact a VHD or VHDX File using PowerShell

2. Mount the VMD or VHDX files as read-only. This is important to do, otherwise the new command might fail. Then use the Optimize-VHD cmdlet (even with vhdx files) and dismount the file when finished.

# Mount temporarily the Example.vhdx file as read-only
Mount-VHD -Path "D:\VMs\Virtual Hard Disks\Example.vhdx" -ReadOnly

# Compact the virtual drive
Optimize-VHD -Path "D:\VMs\Virtual Hard Disks\Example.vhdx" -Mode Full

# Dismount the vhdx file
Dismount-VHD -Path "D:\VMs\Virtual Hard Disks\Example.vhdx"

Reader Interactions

Comments

  1. are you serious? says

    August 18, 2022 at 11:05

    you need hyper-v modules to be installed to use those comandlets and THAT what you forgot to mention

    Reply

Community Questions Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tools

Secondary Sidebar

  • Terms of Use
  • Disclaimer
  • Privacy Policy