Examples: Using Get-VM and Where-Object

I had two cases last week where I needed to get a certain subset of VM’s and specified properties from Virtual Machine Manager.  The first scenario involved SQL servers, which due to our server naming convention was fairly easy.

Get-VM -VMMServer SERVERNAME.DOMAIN.COM | Where-Object {$_.Name -like "*SQL*"}

The next scenario involved using a list of VM’s that was provided in a text file.  I initially struggled with this but eventually got it worked out using the two lines below.  You could also do it in one line but that gets kind of messy.

$VMList = Get-Content C:\scripts\VM.txt 

Get-VM -VMMServer SERVERNAME.DOMAIN.COM | Where-Object {$_.Name -cin $VMList}