Powershell script to merge Hyper-V Virtual Machine disks

In some situations you might find a VM disk to be part of a series of differencing disks. Take this example:


If this was the result of VM Checkpoints, the fix would be easy: simply delete the checkpoints which merges the disks. But his is not the case. Hyper-V Manager shows no checkpoints:


Powershell confirms (notice no output):


This script shuts down the VM and merges its disks. It uses the Get-ParentPath function.

This script can be downloaded from the Microsoft Script Center Repository.

To use this script, download it, run it as administrator, then use the function.

To see help:


For example:
Merge-VMDisks -VMName ‘MyVM’
This may take some time depending on disk sizes and underlying storage system speed.
The script leaves a log file shows the same steps on the console.
This is the picture after the merge is done:
If the script is run on a VM that has no disks to be merged, no changes are made:
The script prompts for confirmation before shutting down the VM:
This can be bypassed by using the -Confirm parameter as shown above.
Script help output looks like:
    Function to merge VM disks
    Merge-VMDisks [-VMName]  [[-LogFile] ] [-WhatIf] [-Confirm] []
    Function/script to merge VM disks. The script will power down the VM in the process.

        Name of the VM whose VHD(x) disks are to be merged
        Required?                    true
        Position?                    1
        Default value                
        Accept pipeline input?       true (ByValue, ByPropertyName)
        Accept wildcard characters?  false
        Name and path of the file where the script will log its steps and progress
        Required?                    false
        Position?                    2
        Default value                ".\Merge-VMDisks_$(Get-Date -format yyyyMMdd_hhmmsstt).txt"
        Accept pipeline input?       false
        Accept wildcard characters?  false
    -WhatIf []
        Required?                    false
        Position?                    named
        Default value                
        Accept pipeline input?       false
        Accept wildcard characters?  false
    -Confirm []
        Required?                    false
        Position?                    named
        Default value                
        Accept pipeline input?       false
        Accept wildcard characters?  false
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer, PipelineVariable, and OutVariable. For more information, see 
        about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). 
        Function by Sam Boutros
        v1.0 - 11/01/2014
    -------------------------- EXAMPLE 1 --------------------------
    C:\PS>Merge-VMDisks -VMName 'v-2012R2-VBR1'

2 responses

  1. Pingback: Running Powershell script functions | Sam's Corner

  2. I’m really enjoying the design and layout of your site. It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a developer to create your theme? Fantastic work!|

    April 25, 2020 at 11:18 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.