Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I've seen (and tried) many ways to skin this cat, but none are quite as effective as I thought and require a number of manual steps. Here is my scenario:

1) I have installed an OS on a Dynamic Virtual Hard Drive via Hyper-V or Boot to VHD on Windows Server 2012 or Windows 8, let's say it takes ~30Gb. 2) I use the VHD, add a lot of files, and then remove them. This expands the VHD to say, ~100Gb.

I want a smooth, simple and reliable operation that does not require prior defragmentation (unless that is a one-step operation), that will recover all the free space on the VM.

My experience in times past is that defragmenting, perhaps zeroing, and then compacting a VHD does not recover all the space and it's quite a messy operation.

As a final option, can I perhaps 'clone' my existing dynamic disk (with the space freed up) to a fresh Dynamic VHD?

I'd be even more pleased if the whole process was able to be scripted via PowerShell.

share|improve this question

1 Answer 1

Create a new VHD, put a fresh file system on it, and copy the files from one to the other. This will create the most compact representation.

And, yes, of course this can be done with PowerShell. See "new-vhd" and "mount-vhd."

share|improve this answer
    
Thank you for the response Jake. I'm looking for something that will avoid missing data due to file permissions, etc. I literally just want everything aside from the empty sectors. Via this method it seems that robocopy might, for example, miss the C:\Users\ directory unless the correct permissions are applied first. I've tried mounting a vhd and using New-VHD to create a new VHD via the parent disk number. I've also tried Convert-VHD, both .vhd <-> .vhdx and .vhd <-> .vhd. It would be nice if that didn't copy empty sectors-but I have a NTFS compressed VHD which is 15Gb utilized, .vhd is 30Gb. –  Aaron - Solution Evangelist Aug 4 '13 at 2:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.