{"id":4813,"date":"2012-10-17T00:01:00","date_gmt":"2012-10-17T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/10\/17\/use-powershell-3-0-to-optimize-hyper-v-vhd-size\/"},"modified":"2012-10-17T00:01:00","modified_gmt":"2012-10-17T00:01:00","slug":"use-powershell-3-0-to-optimize-hyper-v-vhd-size","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-3-0-to-optimize-hyper-v-vhd-size\/","title":{"rendered":"Use PowerShell 3.0 to Optimize Hyper-V VHD Size"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell&nbsp;3.0 to optimize Hyper-V VHD disk size.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. One of my favorite Windows&nbsp;8 features is that (depending on the age of your laptop) you can add real-live, honest-to-goodness Hyper-V. This is such a cool feature because Microsoft Hyper-V technology is awesome. To be able to run it on a laptop with Windows&nbsp;8 is also awesome. What is even better is that Windows&nbsp;8 and Windows Server&nbsp;2012 include an extremely well thought out Hyper-V module for use from within Windows PowerShell&nbsp;3.0. It absolutely rocks.<\/p>\n<p>It seems that with every cool thing, there is always a tradeoff. In my case, it is disk space. I have one laptop that is capable of running Windows&nbsp;8 and Hyper-V, and unfortunately, it has a really small hard disk drive. Even after turning off hibernation support (and thereby reclaiming 8&nbsp;GB of disk space), shrinking my page file, and forming extreme disk cleanup, my laptop is still sucking fumes when it comes to being able to do things like create Word documents or take screenshots.<\/p>\n<p>Another bad thing is that with the Hyper-V module, it is really easy to create VHDs. This also offers a bit of respite.<\/p>\n<p>I keep all of my virtual machines in a folder off the root of my drive C, which is named VMs. To find the size of all of my virtual machines, I use the <b>Get-ChildItem<\/b> cmdlet, and I use a variable to add the length of all of the files. This command is shown here (<b>gci<\/b> is an alias for the <b>Get-ChildItem<\/b> cmdlet. The <b>%<\/b> sign is an alias for the <b>Foreach-Object<\/b> cmdlet). &nbsp;<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; gci -File -Filter *.vhd* -Path C:\\VMs -Recurse | % {$l += $_.length}<\/p>\n<p>I now look at the number that is stored in the <b>$l<\/b> variable. The result is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $l<\/p>\n<p style=\"padding-left: 30px\">54312043008<\/p>\n<p>A great big number with no commas in it is a bit hard to read. I decide to convert the number to gigabytes as shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $l \/ 1gb<\/p>\n<p style=\"padding-left: 30px\">50.5820317268372<\/p>\n<p>It appears that I have over 50 and a half gigabytes of virtual hard disks on my rather small laptop drive. No wonder I am sucking fumes when I attempt to create a new Word document or any other such a thing. I can optimize all of these virtual machine disks and reclaim a bit of disk space. To do this, I use the first command that finds all of the VHDs and pipe it to the <b>Optimize-VHD<\/b> cmdlet.<\/p>\n<p>The <b>Optimize-VHD<\/b> cmdlet will perform multiple types of optimization&mdash;but for now, I am going to do a <i>quick <\/i>optimization. On my laptop, it completes in a couple of minutes (but I do have a fast processor, a lot of memory, and an SSD). As the optimization progresses, a Windows PowerShell progress bar appears in the Windows PowerShell console, as shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6644.hsg-10-17-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6644.hsg-10-17-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The error that presents itself under the progress bar says that <b>Optimize-VHD<\/b> is unable to compact the disk of that particular type. This occurs because one of my VHDs is a fixed disk VHD (not a VHDX) format. It is a small 1-gigabyte disk that I use to permit easy transfer of data between different virtual machines. I attach and detach this virtual hard disk at will. It is a pretty cool idea that I came up with a while back, which helps overcome the problem of not having immediate access to system hard drives. In fact, I could use the VHD to store items I need from the host operating system because, as I talked about in <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/10\/15\/oct-15-blog.aspx\" target=\"_blank\">Use PowerShell to Mount ISO Files in Windows 8<\/a>, I can easily mount a VHD from within Windows&nbsp;8 and Windows Server&nbsp;2012.<\/p>\n<p>I now decide to check the amount of disk space that is consumed by the VHD* drives. I need to initialize the <b>$i<\/b> counter variable that I use, and then I run the same commands as previously by stepping through my command buffer. Here are the commands and the results.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $l=$null<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; gci -File -Filter *.vhd* -Path C:\\VMs -Recurse | % {$l += $_.length}<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $l<\/p>\n<p style=\"padding-left: 30px\">53741617664<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $l \/ 1gb<\/p>\n<p style=\"padding-left: 30px\">50.0507817268372<\/p>\n<p>I now see that I saved a half gig of disk space. Not bad for only a couple minutes of work.<\/p>\n<p>Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell&nbsp;3.0 to optimize Hyper-V VHD disk size. Microsoft Scripting Guy, Ed Wilson, is here. One of my favorite Windows&nbsp;8 features is that (depending on the age of your laptop) you can add real-live, honest-to-goodness Hyper-V. This is such a cool feature because Microsoft Hyper-V [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[271,362,3,367,45,368],"class_list":["post-4813","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-hyper-v","tag-powershell-3","tag-scripting-guy","tag-windows-8","tag-windows-powershell","tag-windows-server-2012"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell&nbsp;3.0 to optimize Hyper-V VHD disk size. Microsoft Scripting Guy, Ed Wilson, is here. One of my favorite Windows&nbsp;8 features is that (depending on the age of your laptop) you can add real-live, honest-to-goodness Hyper-V. This is such a cool feature because Microsoft Hyper-V [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4813","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4813"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4813\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}