{"id":11941,"date":"2011-12-19T15:00:00","date_gmt":"2011-12-19T15:00:00","guid":{"rendered":"https:\/\/aidanfinn.com\/?p=11941"},"modified":"2011-12-19T15:00:00","modified_gmt":"2011-12-19T15:00:00","slug":"hyper-v-powershell-enable-hyper-v","status":"publish","type":"post","link":"https:\/\/aidanfinn.com\/?p=11941","title":{"rendered":"Hyper-V &#038; PowerShell &#8211; Enable Hyper-V"},"content":{"rendered":"<p>Windows 8 includes native PowerShell cmdlets for Hyper-V for the first time ever.&#160; You can use this to manage Hyper-V from the command prompt and to automate complex and\/or repetitive tasks.&#160; In fact, Hyper-V appears to be going down the Exchange route where only so much is revealed in the GUI, and everything is in PowerShell.&#160; I\u2019d expect System Center Virtual Machine Manager to provide that additional GUI functionality (and use PowerShell under the hood).<\/p>\n<p>But before you even get to doing Hyper-V cmdlets, you\u2019ll need to enable the Hyper-V role.&#160; One could use Server Manager, but we\u2019ll use the Server Manager cmdlets instead.&#160; You can learn lots more about this cmdlets in the chapter that I wrote on Server Manager in <a href=\"http:\/\/www.amazon.com\/Mastering-Microsoft-Windows-Server-2008\/dp\/0470532866%3FSubscriptionId%3DAKIAIJ5WNI7ZSH7W4OXA%26tag%3Dafm0c-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0470532866\" target=\"_blank\">Mastering Windows Server 2008 R2<\/a>.<\/p>\n<p>PowerShell cmdlets are a subset of modules.&#160; To use a cmdlet you have to load the relevant module.&#160; Which one?&#160; You can run a cmdlet to find that out:<\/p>\n<p><font style=\"background-color: #666666\">Get-Module \u2013ListAvailable<\/font><\/p>\n<p>You\u2019ll notice that PowerShell works in the format of <em>Verb\u2013Noun (action-thing to manage)<\/em> with optional flags.&#160; In this case it\u2019s retrieving the list of all available modules that can be imported.<\/p>\n<p>Then I can run the following to import the Server Manager module to get access to the relevant cmdlets:<\/p>\n<p><font style=\"background-color: #666666\">Import-Module ServerManager<\/font><\/p>\n<p>Try this: type in half of ServerManager and press &lt;TAB&gt;.&#160; This should autocomplete the text for you.<\/p>\n<p>If I now run the following cmdlet then I get a list of all imported modules:<\/p>\n<p><font style=\"background-color: #666666\">Get-Module<\/font><\/p>\n<p>Note: that if I knew what I was doing, I would have just run the Import-Module cmdlet to import ServerManager.<\/p>\n<p>What cmdlets are not available to me?&#160; There\u2019s a cmdlet for that.&#160; I can run Get-Command but that lists every available cmdlet.&#160; I want to see the ones in the ServerManager module:<\/p>\n<p><font style=\"background-color: #666666\">Get-Command \u2013Module ServerManager<\/font><\/p>\n<p>It turns out that I have:<\/p>\n<ul>\n<li>Get-WindowsFeature: list what features <em>and roles<\/em> are available, and what their install status is (indicated by an X).<\/li>\n<li>Install-WindowsFeature: install a feature <em>or role<\/em><\/li>\n<li>Uninstall-WindowsFeature: install a feature <em>or role<\/em><\/li>\n<\/ul>\n<p>Need some help? That\u2019s built in too:<\/p>\n<p><font style=\"background-color: #666666\">Help Get-WindowsFeature<\/font><\/p>\n<p>We want to know what the state of the machine is so we can run <\/p>\n<p><font style=\"background-color: #666666\">Get-WindowsFeature<\/font><\/p>\n<p><font style=\"background-color: #666666\"><\/font><font style=\"style\">This spits out a list of every role, role service and feature that you can install or uninstall via Server Manager.&#160;&#160;&#160; You should note the name(s) of the ones you want to work with because you you\u2019ll need them for the next step.<\/font><\/p>\n<p>When you check the help of Install-WindowsFeature you\u2019ll see some interesting flags:<\/p>\n<ul>\n<li>-Whatif: It doesn\u2019t run the cmdlet, but it simulates it.&#160; It will tell you (to the best of its ability) if the command will work or not.<\/li>\n<li>-Restart: forces and automatic reboot to happen, rather than prompting for it.&#160; I know that enabling Hyper-V requires a reboot so this is useful.<\/li>\n<\/ul>\n<p>For example this will simulate the install of the Hyper-V role:<\/p>\n<p><font style=\"background-color: #666666\">Install-WindowsFeature Hyper-V \u2013Restart \u2013Whatif<\/font><\/p>\n<p>If I\u2019m happy, and I know a reboot of the new host is OK then I can run:<\/p>\n<p><font style=\"background-color: #666666\">Install-WindowsFeature Hyper-V \u2013Restart<\/font><\/p>\n<p>A progress bar will appear in the PowerShell window, and the new host will reboot so the Hyper-V role can be enabled and started up.<\/p>\n<p>That sounds like we\u2019d done a lot of cmdlet-ing?&#160; We actually could have consolidated it into two commands or a 2 line script:<\/p>\n<p><font style=\"background-color: #666666\">Import-Module ServerManager<\/font><\/p>\n<p><font style=\"background-color: #666666\">Install-WindowsFeature Hyper-V \u2013Restart<\/font><\/p>\n<p>That simple .PS1 script could have been stored in a file share, attached to a Windows deployment, or used in many different ways to automate a Hyper-V enablement.&#160; And that\u2019s just the tip of the iceberg.<\/p>\n<p>What if you want to uninstall Hyper-V?&#160; Well that uninstall is just like the install:<\/p>\n<p><font style=\"background-color: #666666\">Uninstall-WindowsFeature Hyper-V \u2013Restart \u2013Whatif<\/font><\/p>\n<p>Hyper-V is now up and running, and ready to manage.&#160; Sure, you can use the Hyper-V console, and to be honest, that is my primary tool.&#160; But many will want to use PowerShell for automation.&#160; Here\u2019s how you can get started:<\/p>\n<p>Import the modules with:<\/p>\n<p><font style=\"background-color: #666666\">Import-Module Hyper-V<\/font><\/p>\n<p>And list the cmdlets:<\/p>\n<p><font style=\"background-color: #666666\">Get-Command \u2013Module Hyper-V<\/font><\/p>\n<p>There\u2019s lots there to get playing with!<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:0767317B-992E-4b12-91E0-4F059A8CECA8:45fbf69a-337f-45ee-92a1-a70f1ddb4dba\" class=\"wlWriterEditableSmartContent\">Technorati Tags: <a href=\"http:\/\/technorati.com\/tags\/PowerShell\" rel=\"tag\">PowerShell<\/a>,<a href=\"http:\/\/technorati.com\/tags\/Scripting\" rel=\"tag\">Scripting<\/a>,<a href=\"http:\/\/technorati.com\/tags\/Hyper-V\" rel=\"tag\">Hyper-V<\/a>,<a href=\"http:\/\/technorati.com\/tags\/Windows+8\" rel=\"tag\">Windows 8<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Windows 8 includes native PowerShell cmdlets for Hyper-V for the first time ever.&#160; You can use this to manage Hyper-V from the command prompt and to automate complex and\/or repetitive tasks.&#160; In fact, Hyper-V appears to be going down the Exchange route where only so much is revealed in the GUI, and everything is in &hellip; <a href=\"https:\/\/aidanfinn.com\/?p=11941\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Hyper-V &#038; PowerShell &#8211; Enable Hyper-V&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[20],"tags":[181,87,189,109],"class_list":["post-11941","post","type-post","status-publish","format-standard","hentry","category-hyper-v","tag-hyper-v","tag-powershell","tag-scripting","tag-windows-8"],"aioseo_notices":[],"jetpack_featured_media_url":"","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/aidanfinn.com\/index.php?rest_route=\/wp\/v2\/posts\/11941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aidanfinn.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aidanfinn.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aidanfinn.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aidanfinn.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11941"}],"version-history":[{"count":0,"href":"https:\/\/aidanfinn.com\/index.php?rest_route=\/wp\/v2\/posts\/11941\/revisions"}],"wp:attachment":[{"href":"https:\/\/aidanfinn.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aidanfinn.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aidanfinn.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}