New Function: Get VM’s by Powerstate using VMware PowerCLI

Another ususal issue I keep finding is the need to see how many VM’s I have that are either Powered On, Powered Off or Suspended. Today I decided it was time to do two things:

1. Write a script for this so I don’t have to work this out “the hard way”.
2. Make it a function with parameters so that it has help information included for other people and can be modular. This was mainly due to Jonathan Medd and his talk at the last LonVMUG.

So, I decided I needed options to limit the scope of the script. Cluster level seemed like a good start to me, and I also added an option to connect to a particular host/vCenter instance (assumes you are running Powershell as a user with sufficient access permissions).

So, I came up with a script. Then made it into a function. And then fixed it after I broke it! I also realised that outputting large lists of VM’s went over the PowerShell console history length if you had enough VM’s listed, so I added an output to file option to relieve this issue.

If you want to be able to get a list of all VM’s dependant on their current power state, take a copy of this script, save it as a “.psm1” file and import is as a module (import-module). This way you can just run Get-VMByPowerState and you’ll get a full list.

Here’s the function:

function Get-VMByPowerState
        Gets a list of VM's dependant on power state.
        Gets a list of VM's dependant on power state.
    .PARAMETER Cluster
        Name of the cluster to retrieve VM's from. Supports Wildcards.
    .PARAMETER PowerState
        REQUIRED. The power state of the VM's that you are looking for.
		Valid state options are:
		The name or IP of the vCenter or ESX(i) host to connect to. This assumes that you have sufficient access rights as the logged on user.
		Name & path to an output file.
        Get-PowerState -Cluster ClusterName -State PoweredOn -Server -outfile filename.txt
        ,   Position = 0
        ,   ParameterSetName='ByName')]
        $Cluster = '*'
        ,   ParameterSetName='ByDatacenter')]
        ,   ParameterSetName='ByDatacenter')]
        $Server = $null
        ,   ParameterSetName='ByDatacenter')]
		[Alias("Output File Name")]
        $outfile = $null
if ($server -ne $null) {
	connect-viserver $server
$vms = get-vm | where {$_.PowerState -eq $PowerState} | select Name,PowerState
$vmcount = $vms.count
if ($vmcount -gt "0"){
	if ($outfile -eq $null) {
		write-host "List of all $PowerState VM's:"
		write-host "There are $vmcount $PowerState VM's"
	if ($outfile -ne $null){
		write-host "Outputting to file as requested."
		out-file -FilePath $outfile -InputObject $vms}
if ($server -ne $null) {
	disconnect-viserver $server -force:$true -confirm:$false


Recent Posts


McCann & MullenLowe – Microsoft Teams Case Study

When bringing companies with different technology systems together, it can be difficult to efficiently collaborate. #Microsoft Teams can help. Learn how its single platform system provided McCann and MullenLowe with the solution they needed to enable their employees to work together. Check out this video:

Read More »