{"id":2757,"date":"2013-10-08T00:01:00","date_gmt":"2013-10-08T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/10\/08\/using-powershell-to-set-up-hyper-v\/"},"modified":"2013-10-08T00:01:00","modified_gmt":"2013-10-08T00:01:00","slug":"using-powershell-to-set-up-hyper-v","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/using-powershell-to-set-up-hyper-v\/","title":{"rendered":"Using PowerShell to Set Up Hyper-V"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy, Ed Wilson, explains how to use Windows PowerShell to simplify the Hyper-V set-up process.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. I am sitting here sipping a fairly nice cup of green tea. I wimped out this afternoon. I have a team meeting in a few minutes, so I did not take the time to make a proper pot of tea. Instead, I grabbed a tea bag and shoved it into a cup of hot water. No cinnamon, no herbs, no nothing<span>&mdash;only<\/span>&nbsp;hot water and a tea bag. I almost feel like I am being punished.<\/p>\n<p>I paved over my laptop the other day, and I am in the process of setting it up again. I can tell you that it runs WAY faster right now. It boots quickly, shuts down quickly, and everything seems fresh and new. I miss lots of apps, but I am in the process of relocating them in the store, and getting stuff installed.<\/p>\n<p>One project I have been dreading is setting up Hyper-V. I will tell you that having real, live Hyper-V on my desktop operating system is a huge win for me. I used it nearly every day to test some type of script or configuration. I also use it when I am making presentations at conferences such as <a href=\"http:\/\/powershellsaturday.com\/005\/\" target=\"_blank\">PowerShell Saturday in Atlanta<\/a>&mdash;or via Lync, such as <a href=\"http:\/\/powershellsaturday.com\/006\/\" target=\"_blank\">PowerShell Saturday in Singapore<\/a>.<\/p>\n<h2>The thought of all that mousing makes my hand hurt<\/h2>\n<p>Having Hyper-V on my laptop and having a cool client operating system absolutely rocks! But the thought of setting it up and all the associated mousing around makes my hand ache. I mean, dude. There is the setting up of the virtual machine locations, the setting up of the machine configuration storage locations, creating virtual switches&hellip;and that is before I even begin to import any virtual machines. So, naturally, I have been putting it off. I mean, why do today, what I can put off until tomorrow? The Hyper-V user interface is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2477.HSG-10-8-13-01.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2477.HSG-10-8-13-01.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>But, then it hit me. I do not need to put this off (in fact, I cannot put it off too much longer because of Windows PowerShell Saturday in Atlanta) because in addition to having the real-deal Hyper-V, I also have the real-deal Hyper-V Windows PowerShell module. Yes! Windows PowerShell for the win!<\/p>\n<h2>&nbsp;Check to see what I already have<\/h2>\n<p>The first thing one might need to do is to see if Hyper-V is even installed on one&rsquo;s laptop. To check this, use the <strong>Get-WindowsOptionalFeature<\/strong> cmdlet. If you need to enable Hyper-V, you can do so by piping the results to the <strong>Enable-WindowsOptionalFeature<\/strong> cmdlet. This technique is shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-WindowsOptionalFeature -Online -FeatureName *hyper-v*all &nbsp;|<\/p>\n<p style=\"padding-left: 30px\">Enable-WindowsOptionalFeature &ndash;Online<\/p>\n<p>Following the installation of Hyper-V (in addition to the Windows PowerShell management module for Hyper-V), I can use the <strong>Get-VMHost<\/strong> cmdlet to examine my current configuration. I pipe the results to the <strong>Format-List<\/strong> cmdlet so I can see all of the property values. The command and its output are shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-VMHost | Format-List *<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8360.HSG-10-8-13-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8360.HSG-10-8-13-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h2>Creating the virtual machine folders<\/h2>\n<p>One of the cool things about the <strong>MD<\/strong> function in Windows PowerShell is that it can create multiple folders at the same time. Normally, I use the old process as follows:<\/p>\n<ol>\n<li>Test to see if a folder exists.<\/li>\n<li>If the folder exists, exit gracefully.<\/li>\n<li>If the folder does not exist, create it.<\/li>\n<\/ol>\n<p>I use the <strong>Test-Path<\/strong> cmdlet to check for the existence of the folders. But you know what? If I want to create a folder if it does not exist, all I need to do is to call the command to create the folder. If the folder exists, an error occurs. I know this. I really do not need to see a message stating that the folder exists. I really do not care if it does. So I decided to suppress the error message, and pass an array of folder paths to the <strong>MD<\/strong> function.<\/p>\n<h2>Creating the folder paths<\/h2>\n<p>So, I know where I want to create the folders. I decided to break the paths up into a couple of variables, and use the <strong>Join-Path<\/strong> cmdlet to create the folders. This gives me an advantage: I might want to use the script on a different computer at some time, but I still like my folder structure. The only thing I might need to change is the actual drive letter. So in my script, I create a couple of variables as shown here:<\/p>\n<p style=\"padding-left: 30px\">$VMDrive = &#8220;E:&#8221;<\/p>\n<p style=\"padding-left: 30px\">$VMPath = Join-Path -path $VMDrive -ChildPath &#8220;vms\\vm&#8221;<\/p>\n<p style=\"padding-left: 30px\">$VHDPath = Join-Path -Path $VMDrive -ChildPath &#8220;vms\\vhd&#8221;<\/p>\n<p style=\"padding-left: 30px\">MD -Path $vmpath,$VHDPath -ErrorAction 0<\/p>\n<h2>Creating the time span for resource metering<\/h2>\n<p>I wrote a very detailed blog post about creating the time span for resource metering for Hyper-V. You may want to check it out: <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/01\/28\/use-powershell-to-configure-hyper-v-resource-metering.aspx\" target=\"_blank\">Use PowerShell to Configure Hyper-V Resource Metering<\/a>. What I do here, is use the <strong>New-TimeSpan<\/strong> cmdlet to create a new time span of an hour and a half:<\/p>\n<p style=\"padding-left: 30px\">$RMSave = New-TimeSpan -hours 1 -Minutes 30<\/p>\n<h2>Setting the virtual machine host<\/h2>\n<p>To configure my virtual machine host (that is, my laptop), I use the <strong>Set-VMHost<\/strong> cmdlet. I specify the paths and the resource metering timespan. The command shown here is a single line command:<br \/> Set-VMHost -VirtualHardDiskPath $VHDPath -VirtualMachinePath $VMPath -ResourceMeteringSaveInterval $RMSave<\/p>\n<p>When I run the command, no output appears in the output pane of my Windows PowerShell ISE, as shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6327.HSG-10-8-13-03.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6327.HSG-10-8-13-03.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Here is my complete script:<\/p>\n<p style=\"padding-left: 30px\">&lt;#<\/p>\n<p style=\"padding-left: 30px\">&nbsp;ConfigureVMHost.ps1<\/p>\n<p style=\"padding-left: 30px\">&nbsp;Ed Wilson, msft<\/p>\n<p style=\"padding-left: 30px\">&nbsp;HSG-10-8-2013<\/p>\n<p style=\"padding-left: 30px\">#&gt;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"># The following will install Hyper-V on my Win8 laptop<\/p>\n<p style=\"padding-left: 30px\">&lt;#<\/p>\n<p style=\"padding-left: 30px\">Get-WindowsOptionalFeature -Online -FeatureName *hyper-v*all |<\/p>\n<p style=\"padding-left: 30px\">Enable-WindowsOptionalFeature &ndash;Online<\/p>\n<p style=\"padding-left: 30px\">#&gt;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Import-Module -Name Hyper-V<\/p>\n<p style=\"padding-left: 30px\">$RMSave = New-TimeSpan -hours 1 -Minutes 30<\/p>\n<p style=\"padding-left: 30px\">$VMDrive = &#8220;E:&#8221;<\/p>\n<p style=\"padding-left: 30px\">$VMPath = Join-Path -path $VMDrive -ChildPath &#8220;vms\\vm&#8221;<\/p>\n<p style=\"padding-left: 30px\">$VHDPath = Join-Path -Path $VMDrive -ChildPath &#8220;vms\\vhd&#8221;<\/p>\n<p style=\"padding-left: 30px\">MD -Path $vmpath,$VHDPath -ErrorAction 0<\/p>\n<p>&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Set-VMHost -VirtualHardDiskPath $VHDPath -VirtualMachinePath $VMPath -ResourceMeteringSaveInterval $RMSave<\/p>\n<p>Join me tomorrow when I will talk about using Windows PowerShell to work with Hyper-V networking on my laptop.<\/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><strong>Ed Wilson, Microsoft Scripting Guy<\/strong>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, explains how to use Windows PowerShell to simplify the Hyper-V set-up process. Microsoft Scripting Guy, Ed Wilson, is here. I am sitting here sipping a fairly nice cup of green tea. I wimped out this afternoon. I have a team meeting in a few minutes, so I did not [&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,3,45],"class_list":["post-2757","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-hyper-v","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, explains how to use Windows PowerShell to simplify the Hyper-V set-up process. Microsoft Scripting Guy, Ed Wilson, is here. I am sitting here sipping a fairly nice cup of green tea. I wimped out this afternoon. I have a team meeting in a few minutes, so I did not [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2757","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=2757"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2757\/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=2757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}