In This Article
Introduction
Since the release of Windows Server 2012 and Windows 8, Microsoft added a new type and default format of virtual hard disk (VHDX). There are a lot of benefits of using VHDX over VHD files on-premises. I mentioned on-premises, because as of today Azure supports only VHD and not VHDX.
You can read about the improvements and why you should consider using VHDX in Hyper-V whenever possible here.
Microsoft also included two options to convert to VHDX. You can you use Hyper-V Manager Edit Disk option.
Or, you can also use PowerShell to do so using Convert-VHD cmdlet.
In this post, I will show you how to convert multiple VHD files to VHDX format in a single PowerShell One-Liner command.
PowerShell One-Liner To Convert Multiple VHD to VHDX
The following code will convert all VHD files in a source path to VHDX format in a destination path, and finally set the Physical Sector Size Bytes to 4096 (4K).
As a side note, when you create a new VHDX file, it has a physical sector size of 4K by default. However, a converted VHDX file has a physical sector size of 512 Bytes. This step is very important, because the data storage industry will be transitioning the physical format of hard disk drives from 512-byte sectors to 4,096-byte sectors (also known as 4K or 4KB sectors). This transition is driven by several factors. These include increases in storage density and reliability.
<# //----------------------------------------------------------------------- // Copyright (c) {https://charbelnemnom.com}. All rights reserved. //----------------------------------------------------------------------- .SYNOPSIS Converts the format, version type, and block size of a virtual hard disk (VHD) file. .DESCRIPTION Converts the format, version type, and block size of multiple virtual hard disk (VHD) files to VHDX. .NOTES File Name : ConvertTo-VHDX.ps1 Author : Charbel Nemnom Version : 1.1 Date : 10-February-2018 Update : 13-February-2018 Requires : PowerShell Version 4.0 or above OS : Windows Server 2012 R2 or above Product : Microsoft Hyper-V 2012 R2 or above .LINK To provide feedback or for further assistance please visit:Cover Page.EXAMPLE ./ConvertTo-VHDX -Source <SourcePath> -Destination <DestinationPath> -DirectoryName <DirectoryName> This example converts all VHD in a source path to VHDX in a destination path, and finally set the PhysicalSectorSizeBytes to 4096. A converted VHDX file has a physical sector size of 512 Bytes. However, when you create a new VHDX file it has a physical sector size of 4K. #> [CmdletBinding()] param ( [Parameter(Position=0, Mandatory=$true, HelpMessage = 'Source Path')] [Alias('Source')] [String]$SourcePath, [Parameter(Position=1, Mandatory=$true, HelpMessage = 'Destination Path')] [Alias('Destination')] [String]$DestinationPath ) Write-Verbose -Message "Checking the source path..." If (-not(Test-Path -Path "$SourcePath\*" -Filter *.VHD)){ Write-Warning -Message "Source Path does not contain a valid VHD format, Please specify a correct source path!" Exit } Write-Verbose -Message "Checking the destination path..." If (-not(Test-Path -Path "$DestinationPath")){ Write-Warning -Message "Destination Path does not exist, Please specify a correct destination path!" Exit } Write-Verbose -Message "Conversion starts..." # Convert-VHD to VHDX Get-ChildItem -Path "$SourcePath" -Recurse -Filter *.VHD | ` ForEach-Object { Convert-VHD -Path $_.FullName -Destination ("$DestinationPath" + $_.BaseName + ".vhdx") } | ` ForEach-Object { Set-VHD -Path $_.FullName -PhysicalSectorSizeBytes 4096 } [ValidateSet('Yes','No')]$Answer = Read-Host "`nDo you want to delete the source VHD files? Enter Yes/No" If ($Answer -eq 'Yes') { Write-Verbose -Message "Deleting the source VHD files..." Remove-Item -Path "$SourcePath\*" -Recurse -Force }
How to use this tool?
You can run this tool using a single line of PowerShell.
Now you have converted multiple VHD files to the latest virtual machine storage, the VHDX format virtual hard disk.
Where can I download this script?
This script is available on my GitHub repository. You can download it from here. If you have any feedback or changes that everyone should receive, please feel free to update the source and create a pull request.
Hope this helps!
Cheers,
-Charbel