Export Multiple Virtual Machines with Windows PowerShell

Summary: Microsoft PowerShell MVP, Sean Kearney, shows how to easily use Windows PowerShell in Windows Server 2012 to export multiple virtual machines.

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 Sean’s previous blogs.

Sean is a Windows PowerShell MVP and an Honorary Scripting Guy. Sean has been selected to present sessions called Integrating with Microsoft System Center 2012 and Windows PowerShell 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…

Microsoft TechEd is fast approaching. I’m doing work on a presentation, and I decided that I want a quick way for myself and others to easily export a Hyper-V environment.

Sure, you can just jump to the GUI, right-click Export…, and pick a spot.

I prefer consistency. I like to say, “Here’s my virtual machines; please export them.”

To make things more specific, I have created a new virtual network called “MSTechEd.” To make things easier, I have taken my Hyper-V machines that are configured for the conference and attached them to this virtual network by using the following commands:

GET-VM ‘EOT-DC1’ | GET-VMNetworkAdapter | Connect-VMNetworkAdapter –Switchname ‘MSTechEd’

GET-VM ‘EOT-SCORCH’ | GET-VMNetworkAdapter | Connect-VMNetworkAdapter –Switchname ‘MSTechEd’

GET-VM ‘EOT-SCSM’ | GET-VMNetworkAdapter | Connect-VMNetworkAdapter –Switchname ‘MSTechEd’

Now with a common configuration in place, I could simply pull up all my virtual machines based on the network switch name. As I added to my virtual environment, I could simply connect to this network switch, which would provide my consistency.

Now all of my machines for TechEd could be accessed by using this command:

GET-VM | GET-VMNetworkAdapter | where { $_.SwitchName –eq ‘MSTechEd’ }

Oddly, there is a parameter in the Get-VMNetworkAdapter cmdlet called SwitchName, but it is not designed to be used as a filter when piping in data.

So now with an easy way to find specific virtual machines that match my configuration for TechEd, I can run a simple line to export the machines each and every time.

“Wait a minute! How do you find out how to use the cmdlet?” (I can hear many of you shouting up and down the Internet right now.)

So really…

I ran Get-Help on Export-VM asking for examples. That’s how I learn to use cmdlets every time.


And here is the output:

Image of command output

So from Example 2, I should be able to simply add the following command to my first line (where in my case, drive E: is my external USB drive for holding my virtual machines).

GET-VM | GET-VMNetworkAdapter | where { $_.SwitchName –eq ‘MSTechEd’ } | EXPORT-VM –path E:\

Now I can get back to working on my presentation, confident that I have an easy and consistent way to export my virtual machines from Hyper-V.

Feel the Power within you.

The Energized Tech

Thanks once again, Sean! Join me tomorrow for the Weekend Scripter.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy