Get-VM – Retreving a list of VMs in your envirnoment

First thing to remember is to connect to the VCAS.

Connect-VIServer <hostname_of_your_server>

Meet the GET-VM command!

Syntax:

Get-VM [[-Name] <String[]>] [-Server <VIServer[]>] 
    [-Datastore <StorageResource[]>] [-Location <VIContainer[]>]
    [-Tag <Tag[]>] [-NoRecursion] [<CommonParameters>]

If you want to retrieve VMs connected to specific Virtual Switch:

Get-VM [[-Name] <String[]>] [-Server <VIServer[]>] [-VirtualSwitch 
    <VirtualSwitchBase[]>] [-Tag <Tag[]>] [<CommonParameters>]

If you want to retrieve VMs by ID:

Get-VM [-Server <VIServer[]>] -Id <String[]> [<CommonParameters>]

If you want to retrieve VMs by related object:

Get-VM -RelatedObject <VmRelatedObjectBase[]> [<CommonParameters>]

Let’s start to list your VMs. This is general report:

Get-VM

It will produce a list of all VMs. You will see the name, power state, the number of CPU’s, and the amount of memory in GB for each virtual machine:

You can manipulate properties with the Select-Object cmdlet.

Get-VM | Select-Object -Property Name,Notes,VMHost,Guest

The output will look like this:

Name              Notes      VMHost        Guest
----              -----      ------        -----
DC1               DC Server  192.168.1.123 DC1:
VM1                          192.168.1.124 VM1:
DNS1              DNS Server 192.168.1.124 DNS1:

You can also use wildcard characters to select specific virtual machines:

Get-VM -Name A*
Get-VM -Name ??e
Get-VM -Name [bc]*

Using the Where-Object cmdlet, you can set the filter on any property, this command will generate a list of all of your virtual machines that have more than one virtual CPU:

Get-VM | Where-Object {$_.NumCPU -gt 1}

To list VMs in PowerOn state:

(Get-VM).where{$_.PowerState -eq 'PoweredOn'}

To list VMs in PowerOn state and with Windows OS:

(Get-VM).where{$_.PowerState -eq 'PoweredOn' -and $_.Guest.OSFullName -match 'Windows'}

I hope those few examples will help you with Get-VM.

If you need explanation or you do have any questions related to this script, please do not hesitate to leave the comment.

Visit my FB page: https://www.facebook.com/AngrySysOps

Subscribe to my YouTube channel: https://www.youtube.com/channel/UCRTcKGl0neismSRpDMK_M4A

Please leave the comment