PowerShell – Find all VMs running on all your HyperV servers

Supose you have many HyperV servers [standalone and clusters] and you would like to collect all the VM names and their current statuses [like running, stopped, etc.]. Powershell script is in great help here, it will give you the answer in few seconds.

I’m going to use WMI. Why? Because it allows me to query remote servers whether they have Powershell installed or not (to be hounest you could use VBScript here, but these days if you are using PS you are cool 🙂

# This script collects VM names and status from a list of Hyper-V nodes [in a $HyperV_Servers variable]
# It generates two text files, one for quering SQL and one for Excel

# This to convert state codes to a meaningfull names

function ConvertStateToFriendly([int] $enabledstate)
  0 {return “Unknown”}
  2 {return “Enabled”}
  3 {return “Stopped”}
  32768 {return “Paused”}
  32769 {return “Suspended”}
  32770 {return “Starting”}
  32771 {return “Snapshotting”}
  32772 {return “Migrating”}
  32773 {return “Saving”}
  32774 {return “Stopping”}
  32775 {return “Deleted”}
  32776 {return “Pausing”}

# Array of HyperV servers:
$HyperV_Servers = “Host1″,”Host2″,”Host3”

# Initiating
$ResultForSQL = “”

foreach($HyperV in $HyperV_Servers)
 Write-Host $HyperV
 # Connecting to a Msvm_ComputerSystem WMI
 $vms = Get-WmiObject -Class Msvm_ComputerSystem -Namespace “root\virtualization” -ComputerName $HyperV

 # Filtering out host records
 $vms = $vms | where-object{$_.caption -ne “Hosting Computer System”}

 # For every detected VM we are printing Name and State values
 foreach($VmsObj in $vms)
  # Converts number to a friendly state name
  $statefriendly = convertStateToFriendly $VmsObj.EnabledState
  # Generates detailed list
  $Result = $HyperV + “,” + $VmsObj.ElementName + “,” + $statefriendly
  $Result >> vmswithstate.txt
  # Accumulates comma separated list of VMs in the format prepared to pass it to a SQL query
  $ResultForSQL = “‘” + $VmsObj.ElementName + “‘” +  “,” + $ResultForSQL
  # Output to a console
  Write-Host  $VmsObj.ElementName `t $statefriendly #`t $VmsObj.Name #”   ” $Vms_memObj.InstanceID

# Prints a comma separated SQL prepared list of server names to a file
$ResultForSQL > VMListforSQL.txt

Change the HyperV host names stored in $HyperV_Servers variable to yours.

Few words about the output it generates. I create here 3 outputs: text file for a reporting purposes (let say you need to send it to somebody), coma separated list for a further requests (I used it to create a SQL query in my SCCM), to a PS console to the output.


Leave a Comment here

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s