{"id":3546,"date":"2013-05-31T00:01:00","date_gmt":"2013-05-31T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/05\/31\/import-multiple-virtual-machines-with-windows-powershell\/"},"modified":"2013-05-31T00:01:00","modified_gmt":"2013-05-31T00:01:00","slug":"import-multiple-virtual-machines-with-windows-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/import-multiple-virtual-machines-with-windows-powershell\/","title":{"rendered":"Import Multiple Virtual Machines with Windows PowerShell"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Microsoft PowerShell MVP, Sean Kearney, talks about importing multiple virtual machines with Windows PowerShell in Windows Server&nbsp;2012.<\/span><\/p>\n<p><span style=\"font-size: 12px\">Microsoft Scripting Guy, Ed Wilson, is here. If you are a seasoned Hey, Scripting Guy! Blog reader, you know that the most frequent guest blogger is Sean Kearney. If you are new to the blog, I welcome you, and I encourage you to catch up with <\/span><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/sean+kearney\/\" target=\"_blank\">Sean&rsquo;s previous blogs<\/a><span style=\"font-size: 12px\">.<\/span><\/p>\n<p>Sean is a Windows PowerShell MVP and <a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/honorary-scripting-guy-award-recipients-announced\/\" target=\"_blank\">an Honorary Scripting Guy.<\/a> Sean has been selected to present sessions called <a href=\"https:\/\/channel9.msdn.com\/Events\/TechEd\/NorthAmerica\/2013\/MDC-B326#fbid=rHDRO4Syj3v\" target=\"_blank\">Integrating with Microsoft System Center 2012 and Windows PowerShell<\/a> at TechEd NA and TechEd Europe this year. In his free time, Sean has written several blog posts about Hyper-V and some other cool stuff, and for the next few weeks, Sean will be the designated guest blogger on Fridays. Take it away Sean&hellip;<\/p>\n<p>Now let&rsquo;s get back to my problem&hellip;some consistency for TechEd.<\/p>\n<p>In my last blog, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/05\/24\/export-multiple-virtual-machines-with-windows-powershell.aspx\" target=\"_blank\">Export Multiple Virtual Machines with Windows PowerShell<\/a>, I had a nice easy and consistent way of exporting virtual machines from Hyper-V. Now of course, I would like to reverse that process. Wouldn&#8217;t it be beautiful if I could simply point to a folder of virtual machines and say, &ldquo;Import!&rdquo;<\/p>\n<p>Well, with Windows PowerShell, I can.<\/p>\n<p>My first task was to see how the <strong>Import-VM<\/strong> cmdlet worked. So off to my favorite friend in the whole wide world (next to Doctor Scripto, that is)&mdash;<strong>Get-Help<\/strong>.<\/p>\n<p style=\"padding-left: 30px\">GET-HELP IMPORT-VM<\/p>\n<p>And look at the output it brings me:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2437.hsg-5-31-13-1.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2437.hsg-5-31-13-1.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>So I sat down to play with the <strong>Import-VM<\/strong> cmdlet to see what worked best for me. I know I can just &ldquo;run with defaults&rdquo; as in the following example:<\/p>\n<p style=\"padding-left: 30px\">GET-HELP IMPORT-VM -examples<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0172.hsg-5-31-13-2.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0172.hsg-5-31-13-2.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>However, I prefer to have control and be specific about all the details. So first I wanted to know the physical location of my Hyper-V defaults for the path of the virtual machine configuration and the virtual machine hard disks.<\/p>\n<p>To obtain this information, you can run the <strong>Get-VMHost<\/strong> cmdlet:<\/p>\n<p style=\"padding-left: 30px\">$VMhost=GET-VMhost<\/p>\n<p>When I had this stored away, I ran <strong>Get-Member<\/strong> to find which of the available properties might have the correct details.<\/p>\n<p style=\"padding-left: 30px\">$VMHOST | GET-MEMBER &ndash;membertype Property<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7103.hsg-5-31-13-3.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7103.hsg-5-31-13-3.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Near the bottom, I could see two properties that matched my liking: <strong>VirtualHardDiskPath<\/strong> and <strong>VirtualMachinePath<\/strong>. I confirmed by piping them through <strong>Select-Object<\/strong> to see if they had the data I needed:<\/p>\n<p style=\"padding-left: 30px\">$VMHost | SELECT-OBJECT VirtualHardDiskPath, VirtualMachinePath<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2845.hsg-5-31-13-4.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2845.hsg-5-31-13-4.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>They had what I needed, so I stored them away for future use.<\/p>\n<p style=\"padding-left: 30px\">$VMDefaultPath=$VMHost.VirtualMachinePath<\/p>\n<p style=\"padding-left: 30px\">$VMDefaultDrive=$VMHost.VirtualHardDiskPath<\/p>\n<p>The next challenge wasn&rsquo;t so tricky&hellip;point at a folder structure of a virtual machine (or machines for that matter) and identify each .exp file from the original exports. Hello <strong>Get-ChildItem<\/strong>:<\/p>\n<p style=\"padding-left: 30px\">$VMLIST=GET-CHILDITEM G:\\Export -recurse &ndash;include *.exp<\/p>\n<p>&hellip;where <strong>G:\\Export<\/strong> is my folder that contains a virtual machine export or exports from Hyper-V.<\/p>\n<p>I then played with the Import-VM cmdlet to get the right combination to match a default import of a virtual machine to a defined hard disk and configuration path. Spending this extra time meant that I could easily adapt the script later to point to nonstandard paths if I so choose.<\/p>\n<p>I could then step through a list of machines found in the following manner:<\/p>\n<p style=\"padding-left: 30px\">$VMlist | FOREACH { IMPORT-VM -path $_.Fullname -Copy -VhdDestinationPath $VMDefaultDrive -VirtualMachinePath $VMDefaultPath -SnapshotFilePath $VMDefaultPath -SmartPagingFilePath $VMDefaultPath -GenerateNewId }<\/p>\n<p>With this in place, I can always have a consistent way of importing my virtual machines. When done, I simply attach these machines to the current network by using the <strong>Connect-VMSwitch<\/strong> cmdlet.<\/p>\n<p>The coolest part is it worked just as well with Windows Server&nbsp;2008&nbsp;R2 exports, providing there were no Hyper-V snapshots.<\/p>\n<p>Feel the Power within you.<\/p>\n<p>~Sean<br \/> The Energized Tech<\/p>\n<p>Thank you, Sean, for a great post. Join me tomorrow for 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><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft PowerShell MVP, Sean Kearney, talks about importing multiple virtual machines with Windows PowerShell in Windows Server&nbsp;2012. Microsoft Scripting Guy, Ed Wilson, is here. If you are a seasoned Hey, Scripting Guy! Blog reader, you know that the most frequent guest blogger is Sean Kearney. If you are new to the blog, I welcome [&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":[56,271,3,154,130,45],"class_list":["post-3546","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-hyper-v","tag-scripting-guy","tag-sean-kearney","tag-servers","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft PowerShell MVP, Sean Kearney, talks about importing multiple virtual machines with Windows PowerShell in Windows Server&nbsp;2012. Microsoft Scripting Guy, Ed Wilson, is here. If you are a seasoned Hey, Scripting Guy! Blog reader, you know that the most frequent guest blogger is Sean Kearney. If you are new to the blog, I welcome [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3546","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=3546"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3546\/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=3546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}