Hyper-V–I need to Merge my AVHD and VHD–What to do?!

First rule.  DO NOT Take a Snapshot in your Production environment.  Avoid it at all costs whether it’s VMware or Hyper-V.  It’s a massive risk to the data if corruption occurs.

Now if you IGNORED this rule or you HAD to do something that INSISTED on this happening, don’t sweat it.

The problem is sometimes we (as humans) make mistakes.  The snapshot gets taken *and* get’s forgotten about.  So you have to Merge it.

Normally in Hyper-V if you do a shutdown of the Virtual Machine, the environment WILL automatically start merging.  This is the norm.  

But factors can affect this.  Space, Murphy and the Unknown are the uncontrollable factors we Professionals have to deal with.  However it’s good to know there is a fallback.  You can do it manually.


Yes, manually.   Now no matter how much data you have, no matter how long it takes… BACK IT UP FIRST!!!

I say this because you always (no matter your skillset, no matter what you think SHOULD happen) you have to plan for the UNexpected.  Power outage, corrupt files on the server, the Host dying during the process.  Fortunately in my experience Hyper-V is well designed for the unexpected.

Let’s also think about just what an AVHD file is.   It is closer to a list of transactions than a virtual hard drive.   So when Hyper-V goes to merge the data, it is examining the transactions and building the drive structure to commit it.  If you lose power, you still shouldn’t lose data.

But if you need to do a manual merge (Perhaps you would like to rebuild those files offline to avoid issues?) this is also a simple process. (Although scary the first time you try it!)

Rename all the “AVHD” files to VHD

Go to Hyper-V and choose “EDIT DISK”.  Browse to the folder of the files.

Find the NEWEST file (That was an AVHD and is now a VHD) and select it.

You MAY get an option to “Reconnect to Parent”.  This can be normal if you have MULTIPLE Avhd files.   Take note of the ORIGINAL filename and connect to that file in your current folder (if you read the screen Hyper-V is actually prompting you).  The AVHD’s refer to the original VHD (if they are the first Snapshot *or* they will refer to the PREVIOUS Avhd file.

If you get a Reconnect, the choose the proper file and back to EDIT and REselect the Newest file again.   You will now be presented with “Compact” or “Merge” as your options.  Choose “Merge”

Let the file Merge (Depending on the size it could take a bit, don’t worry and don’t panic)

Repeat the process going back a file and back a file until you only have you VHD

If you’re concerned about the content afterwards and don’t want to chance a boot, Remember in Server 2008R2 and Server 2008 in DiskMgmt you can attach the VHD in Readonly mode to verify the content.

Now in my case my Hyper-V files are well organized.  You may find out yours are still in the default location like C:ProgramDataMicrosoftHyper-V .  But one of the nice things about the AVHD files (Snapshots) is they mimic the name of the VHD file.  so if your Parent VHD was called


you’ll find the AVHD files will have names like.


If you have to go digging, you usually don’t.

Also some more details I found out.

600 gigabytes on a Strong Raid 5 controller over SAN is about 6 hours to Merge.  I found similar numbers when working directly from Raid 5 Sata drives.

It will Merge One AVHD file at a time.  this is Good to know because if you’re watching you WILL actually see the AVHD files disappear, Newest to oldest.   (As the Newest is Merged INTO it’s predecessor)

It does NOT build a “NEW” Vhd file, so disk space (As far as I can tell) is NOT an issue unless the parent is meant to “Grow” in the process.   But between the Parent and the Removed child, you should be ok…

So just remember.   Don’t panic if you have a massive amount to Merge.  It will happen.  If you’re nervous, copy the files ELSEWHERE and begin doing a Manual merge of the backups in Hyper-V.  The good part there is you can mount it all afterwards to verify the status.

…. Most of all just be cautious, but be confident.  It will all work out.

Hyper-V–“Oh NO! I extended Disk space and it DIDN’T SHOW UP!”

Here’s an easy “Don’t panic!” situation.

You’ve got a VHD file.  You’ve adjusted it to allow more space in the “Edit Disk”.   When you go into Server 2008 / 2008R2 and Extend the free space in DiskMgmt you get some confusion.

The DiskMgmt utility shows the Correct number but the File system shows the old number.  Rebooting does fix it either!

So… before you go running to the Datacenter cursing the gods, try this.

Odds are, the server was a high usage thing with Big files (Like Exchange or SQL etc etc)

Before you reboot, make sure anything that can cause it to “Get busy” is switched to Manual (Sql Instance, Exchange Datastore) and reboot.  Do this in the safest manner of course.

Down the server again and adjust the space a bit larger (Any amount, 1 gb or 20 gb) and then power the machine up again.  

Now that the computer is running in a say “Less frantic” state try re-extending the partition.  Odds are it’ll work this time. 

I ran into this and I’m presuming it’s a case of the Partition table was updated but the File Allocation wasn’t.   I’m not sure on the DEEP details but here’s what worked for me

Maybe it’ll help you out.

”The Energized Tech”

Implementing the–WHATIF into an Advanced Function in Windows Powershell

So you think you know how to make an Advanced Function.  I thought I did.   Then I started messing about with HANDLE.EXE from Sysinternals and was REALLY getting into automating that baby with Pipelines and whatnot.

It was all working so beautifully UNTIL….

That’s right “Until…” Until I accidentally ended closing ALL the files in the entire O/S with a typo (well most of them!)

It was then I decided that until I got it so you could pass a –whatif properly, I wasn’t sharing my new toy with the world as it would have been a dangerous release indeed.   There is a VERY important reason Mr. Russinovich did NOT release a utility that in ONE LINE would have closed everything on a server.

But thanks to Powershell, we can pull out his Safety net, automate it and bring in a New one.

the Trick with Using the –whatif is enabling “CmdletBinding” in the Advanced Function and Marking “SupportsShouldProcess” as $TRUE

Something like this

Function global:Close-Openfile()

This of course is just the top of a function called “Close-OpenFile” that has the “CmdletBinding” enabled with SupportsShouldProcess.

With this on it’s very simple to use the “-whatif” parameter.  You need only add in a simple line like this in your Process block

if ( $PSCmdlet.ShouldProcess(‘Jibberish’) ) { BlockofCodeToExecuteIfNO-Whatif }

It messed me up the first time.   The $PSCmdlet.ShouldProcess() method returns a Boolean $TRUE if “whatif” is NOT in use.  If it *IS* then it returns a Boolean $FALSE and outputs a statement similar to this where ‘Jibberish’ is replaced with WHATEVER you want in there that makes sense

What if: Performing operation "Close-Openfile" on Target "Jibberish".

Neat eh?   First time I did it, I got the code backwards and closed everything.   But there you go. 

“-whatif” is EASY to implement and the GREATEST feature of Windows Powershell, A live failsafe !

Special Thanks to Kirk “Poshoholic” Munro and Shay Levy for the help in educating me.  An especially big thanks to James Brundage for being a soundboard for other things Smile

The Power of Shell is in all of us. RELEASE IT!

the Energized Tech