{"id":2102,"date":"2019-07-30T14:39:22","date_gmt":"2019-07-30T19:39:22","guid":{"rendered":"http:\/\/www.wildow.com\/blog\/?p=2102"},"modified":"2019-08-01T09:39:44","modified_gmt":"2019-08-01T14:39:44","slug":"convert-resize-and-optimize-vhd-and-vhdx-files-with-powershell","status":"publish","type":"post","link":"http:\/\/www.wildow.com\/blog\/?p=2102","title":{"rendered":"Convert, Resize, and Optimize VHD and VHDX files with PowerShell"},"content":{"rendered":"<header class=\"entry-header\">\n<h1 class=\"entry-title\">Convert, Resize, and Optimize VHD and VHDX files with PowerShell<\/h1>\n<div class=\"entry-byline\"><span class=\"entry-author vcard\"><a class=\"url fn n\" title=\"Posts by Mike F Robbins\" href=\"https:\/\/mikefrobbins.com\/author\/mikefrobbins\/\" rel=\"author\"><span class=\"author-name\">MIKE F ROBBINS<\/span><\/a><\/span>\u00a0<span class=\"entry-date\"><time class=\"published\" datetime=\"2017-03-23T07:30:31-05:00\">MARCH 23, 2017<\/time><\/span>\u00a0<a class=\"comments-link\" href=\"https:\/\/mikefrobbins.com\/2017\/03\/23\/convert-resize-and-optimize-vhd-and-vhdx-files-with-powershell\/#comments\">1<\/a><\/div>\n<\/header>\n<div class=\"entry-content\">\n<p>I recently received an email from someone who attended one of my presentations asking if I had a blog article on using PowerShell to compact and optimize VHD files. Since I didn\u2019t have a blog article on that subject, I decided to create one.<\/p>\n<p>The process itself is fairly simple. The examples shown in this blog article are being run on a Windows 10 computer which has Hyper-V enabled on it. Only specific SKU\u2019s of Windows 10 are capable of running Hyper-V. The same process can be used on servers running Windows Server 2012 R2 or Windows Server 2016 (and possibly on other versions, but I can\u2019t confirm that).<\/p>\n<p>There are several PowerShell cmdlets for working with VHD files as shown in the following example.<\/p>\n<div id=\"crayon-5d3fc1dc28537520186897\" class=\"crayon-syntax crayon-theme-powershell-ise crayon-font-droid-sans-mono crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover disable-anim\">\n<div class=\"crayon-plain-wrap\"><\/div>\n<div class=\"crayon-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"crayon-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"crayon-nums-content\">\n<div class=\"crayon-num\" data-line=\"crayon-5d3fc1dc28537520186897-1\">1<\/div>\n<\/div>\n<\/td>\n<td class=\"crayon-code\">\n<div class=\"crayon-pre\">\n<div id=\"crayon-5d3fc1dc28537520186897-1\" class=\"crayon-line\"><span class=\"crayon-r \">Get-Command<\/span> <span class=\"crayon-cn\">-Noun<\/span> <span class=\"crayon-i\">VHD<\/span><span class=\"crayon-o\">*<\/span> <span class=\"crayon-cn\">-Module<\/span> <span class=\"crayon-i\">Hyper<\/span><span class=\"crayon-cn\">-V<\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p><a href=\"http:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd1a.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15124\" src=\"http:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd1a.png\" sizes=\"auto, (max-width: 859px) 100vw, 859px\" srcset=\"https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd1a.png 859w, https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd1a-300x99.png 300w, https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd1a-768x254.png 768w, https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd1a-250x83.png 250w\" alt=\"\" width=\"859\" height=\"284\" \/><\/a><\/p>\n<p>The\u00a0<em>Convert-VHD<\/em>\u00a0cmdlet can be used to change the format (VHD to VHDX or vise-versa), Type (fixed, dynamic, and differencing), and block size of the file.<\/p>\n<p>The following example converts a VHD file to a VHDX file.<\/p>\n<div id=\"crayon-5d3fc1dc2854a524512881\" class=\"crayon-syntax crayon-theme-powershell-ise crayon-font-droid-sans-mono crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover disable-anim\">\n<div class=\"crayon-plain-wrap\"><\/div>\n<div class=\"crayon-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"crayon-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"crayon-nums-content\">\n<div class=\"crayon-num\" data-line=\"crayon-5d3fc1dc2854a524512881-1\">1<\/div>\n<\/div>\n<\/td>\n<td class=\"crayon-code\">\n<div class=\"crayon-pre\">\n<div id=\"crayon-5d3fc1dc2854a524512881-1\" class=\"crayon-line\"><span class=\"crayon-r \">Convert-VHD<\/span> <span class=\"crayon-cn\">-Path<\/span> <span class=\"crayon-s\">&#8216;D:\\Hyper-V\\Virtual Hard Disks\\nano2.vhd&#8217;<\/span> <span class=\"crayon-cn\">-DestinationPath<\/span> <span class=\"crayon-s\">&#8216;D:\\Hyper-V\\Virtual Hard Disks\\nano2.vhdx&#8217;<\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p><a href=\"http:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd2a.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15126\" src=\"http:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd2a.png\" sizes=\"auto, (max-width: 859px) 100vw, 859px\" srcset=\"https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd2a.png 859w, https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd2a-300x46.png 300w, https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd2a-768x118.png 768w, https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd2a-250x38.png 250w\" alt=\"\" width=\"859\" height=\"132\" \/><\/a><\/p>\n<p>There are a number of parameters for the\u00a0<em>Convert-VHD<\/em>\u00a0cmdlet so be sure to take a look at the\u00a0<a href=\"https:\/\/technet.microsoft.com\/en-us\/itpro\/powershell\/windows\/hyper-v\/convert-vhd\" target=\"_blank\" rel=\"noopener\">help<\/a>\u00a0for it. The\u00a0<em>BlockSizeBytes<\/em>\u00a0parameter is used to change the block size, the\u00a0<em>DeleteSource<\/em>\u00a0parameter is used to delete the source file once the destination file is created, and the VHDType parameter is used to change the type of VHD\u00a0(fixed, dynamic, or differencing).<\/p>\n<p>One thing to keep in mind is the conversion process is an offline operation. The help states that the file being converted can\u2019t be attached, but what I\u2019ve found is that it can be attached as long as the VM (virtual machine) isn\u2019t running.<\/p>\n<p>The\u00a0<em>Optimize-VHD<\/em>\u00a0cmdlet is used to optimize the amount of space used by dynamic virtual hard drive files. When this cmdlet is run, it compacts the virtual file as shown in the following example.<\/p>\n<div id=\"crayon-5d3fc1dc28558171663455\" class=\"crayon-syntax crayon-theme-powershell-ise crayon-font-droid-sans-mono crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover disable-anim\">\n<div class=\"crayon-plain-wrap\"><\/div>\n<div class=\"crayon-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"crayon-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"crayon-nums-content\">\n<div class=\"crayon-num\" data-line=\"crayon-5d3fc1dc28558171663455-1\">1<\/div>\n<\/div>\n<\/td>\n<td class=\"crayon-code\">\n<div class=\"crayon-pre\">\n<div id=\"crayon-5d3fc1dc28558171663455-1\" class=\"crayon-line\"><span class=\"crayon-r \">Optimize-VHD<\/span> <span class=\"crayon-cn\">-Path<\/span> <span class=\"crayon-s\">&#8216;D:\\Hyper-V\\Virtual Hard Disks\\srv1e.vhd&#8217;<\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p><a href=\"http:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd3a.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15129\" src=\"http:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd3a.png\" sizes=\"auto, (max-width: 859px) 100vw, 859px\" srcset=\"https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd3a.png 859w, https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd3a-300x46.png 300w, https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd3a-768x117.png 768w, https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd3a-250x38.png 250w\" alt=\"\" width=\"859\" height=\"131\" \/><\/a><\/p>\n<p>Based on the information found in the\u00a0<a href=\"https:\/\/technet.microsoft.com\/en-us\/itpro\/powershell\/windows\/hyper-v\/optimize-vhd\" target=\"_blank\" rel=\"noopener\">help<\/a>, this cmdlet not only reclaims unused blocks, but it also rearranges the blocks to be more efficiently packed, which also reduces the size of the VHD or VHDX. It\u2019s possible for this command to complete without shrinking the virtual file if no optimization is necessary. The Mode parameter can be used to change the default mode which is Full for VHD files and Quick for VHDX files.<\/p>\n<p>The\u00a0<em>Resize-VHD<\/em>\u00a0cmdlet is used to shrink VHDX files or expand both VHD and VHDX files. The shrink operation will fail if a size smaller than the minimum size is specified as shown in the following example.<\/p>\n<div id=\"crayon-5d3fc1dc28564968123868\" class=\"crayon-syntax crayon-theme-powershell-ise crayon-font-droid-sans-mono crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover disable-anim\">\n<div class=\"crayon-plain-wrap\"><\/div>\n<div class=\"crayon-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"crayon-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"crayon-nums-content\">\n<div class=\"crayon-num\" data-line=\"crayon-5d3fc1dc28564968123868-1\">1<\/div>\n<\/div>\n<\/td>\n<td class=\"crayon-code\">\n<div class=\"crayon-pre\">\n<div id=\"crayon-5d3fc1dc28564968123868-1\" class=\"crayon-line\"><span class=\"crayon-r \">Get-VHD<\/span> <span class=\"crayon-cn\">-Path<\/span> <span class=\"crayon-s\">&#8216;D:\\Hyper-V\\Virtual Hard Disks\\srv1e.vhd&#8217;<\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p><a href=\"http:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd4a.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15131\" src=\"http:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd4a.png\" sizes=\"auto, (max-width: 859px) 100vw, 859px\" srcset=\"https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd4a.png 859w, https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd4a-300x112.png 300w, https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd4a-768x287.png 768w, https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd4a-250x93.png 250w\" alt=\"\" width=\"859\" height=\"321\" \/><\/a><\/p>\n<p>The\u00a0<em>MinimumSize<\/em>\u00a0parameter can be used to shrink a VHDX to it\u2019s minimum size without having to worry about the possibility of specifying a file size that\u2019s too small.<\/p>\n<div id=\"crayon-5d3fc1dc2856f013390965\" class=\"crayon-syntax crayon-theme-powershell-ise crayon-font-droid-sans-mono crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover disable-anim\">\n<div class=\"crayon-plain-wrap\"><\/div>\n<div class=\"crayon-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"crayon-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"crayon-nums-content\">\n<div class=\"crayon-num\" data-line=\"crayon-5d3fc1dc2856f013390965-1\">1<\/div>\n<\/div>\n<\/td>\n<td class=\"crayon-code\">\n<div class=\"crayon-pre\">\n<div id=\"crayon-5d3fc1dc2856f013390965-1\" class=\"crayon-line\"><span class=\"crayon-r \">Resize-VHD<\/span> <span class=\"crayon-cn\">-Path<\/span> <span class=\"crayon-s\">&#8216;D:\\Hyper-V\\Virtual Hard Disks\\srv1.vhdx&#8217;<\/span> <span class=\"crayon-cn\">-ToMinimumSize<\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p><a href=\"http:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd5a.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15136\" src=\"http:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd5a.png\" sizes=\"auto, (max-width: 859px) 100vw, 859px\" srcset=\"https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd5a.png 859w, https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd5a-300x20.png 300w, https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd5a-768x52.png 768w, https:\/\/mikefrobbins.com\/wp-content\/uploads\/2017\/03\/optimize-vhd5a-250x17.png 250w\" alt=\"\" width=\"859\" height=\"58\" \/><\/a><\/p>\n<p>Be sure to take a look at the\u00a0<a href=\"https:\/\/technet.microsoft.com\/en-us\/itpro\/powershell\/windows\/hyper-v\/resize-vhd\" target=\"_blank\" rel=\"noopener\">help<\/a>\u00a0for the\u00a0<em>Resize-VHD<\/em>\u00a0cmdlet to determine all of the available options.<\/p>\n<\/div>\n<p>other links<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"yDwLnoHhjw\"><p><a href=\"https:\/\/deploymentbunny.com\/2015\/06\/15\/powershell-is-king-compress-compact-optimize-vhd-and-vhdx-files\/\">PowerShell is King &#8211; Compress, Compact, Optimize VHD and VHDX&nbsp;files<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;PowerShell is King &#8211; Compress, Compact, Optimize VHD and VHDX&nbsp;files&#8221; &#8212; The Deployment Bunny\" src=\"https:\/\/deploymentbunny.com\/2015\/06\/15\/powershell-is-king-compress-compact-optimize-vhd-and-vhdx-files\/embed\/#?secret=4IUUYy6Ywa#?secret=yDwLnoHhjw\" data-secret=\"yDwLnoHhjw\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p><a href=\"https:\/\/gallery.technet.microsoft.com\/scriptcenter\/Powershell-Script-to-ae5c00ba\">https:\/\/gallery.technet.microsoft.com\/scriptcenter\/Powershell-Script-to-ae5c00ba<\/a><\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/hyper-v\/optimize-vhd?view=win10-ps\">https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/hyper-v\/optimize-vhd?view=win10-ps<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Convert, Resize, and Optimize VHD and VHDX files with PowerShell MIKE F ROBBINS\u00a0MARCH 23, 2017\u00a01 I recently received an email from someone who attended one of my presentations asking if I had a blog article on using PowerShell to compact &#8230; <a class=\"more-link\" href=\"http:\/\/www.wildow.com\/blog\/?p=2102\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,16],"tags":[],"class_list":["post-2102","post","type-post","status-publish","format-standard","hentry","category-backup","category-hyperv"],"_links":{"self":[{"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2102"}],"version-history":[{"count":3,"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2102\/revisions"}],"predecessor-version":[{"id":2106,"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2102\/revisions\/2106"}],"wp:attachment":[{"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2102"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}