Summary: Learn how to use Windows PowerShell 3.0 in Windows Server 2012 to obtain a list of 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…
So you’ve got the new Hyper-V environment, and you need to get a list of those virtual machines. Life is smooth and easy…
Let’s think about this, you want to “get” a “virtual machine” list. Would anybody like to guess just how easy this one is?
In Windows Server 2012 with the built-in Hyper-V, all you need to run is:
Get-VM
This will output a list of virtual machines and their current state to the Windows PowerShell console.
If you’d like to filter and show only virtual machines that are currently in the running state, you can apply this command in the console:
Get-VM | Where { $_.State –eq ‘Running’ }
We can use this and save the state of those workstations:
Get-VM | Where { $_.State –eq ‘Running’ } | Save-VM
This provides the same result as if you go into the Hyper-V console, right-click the names of individual workstations, and click Save.
If you have administrative credentials, you can even manage remote workstations running Windows Server 2012 with Hyper-V in this manner:
Get-VM –computername ‘Contoso-HyperV-1’ | Where { $_.State –eq ‘Running’ } | Save-VM
With this, you can also use our good friend Export-CSV to produce a file with the list of virtual machines:
Get-VM –computername ‘Contos-HyperV-1’ | EXPORT-CSV C:\Powershell\VMlist.csv
The best part is that not only are these cmdlets built in, but they operate in the free Hyper-V Server 2012. There’s more of course, but let’s take one piece at a time…
Feel the Power within you.
~Sean
The Energized Tech
Thanks, Sean. Sean will be running a weekly series about Hyper-V and Windows PowerShell for the next several months. Join him next Friday, and join me tomorrow for more Windows PowerShell goodness.
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