VMware PowerCLI Pocket Guide

I have put together this VMware PowerCLI Pocket Guide with the commands I use daily. I will constantly keep adding commands to this page. If you have some nice PowerCLI scripts that you wish to share on this page, please contact me.

Connecting to vCenter

connect-viserver (Connects your PowerCLI session to vCenter)


$vmhosts = Get-VMHost -location (Get the names of all hosts in a cluster and place them in the variable $vmhosts)
$vmhosts = Get-VMHost | where { $_.Name -ne “vmhost1.vmlab.local” } | where { $_.Name -ne “vmhost2.vmlab.local” } (Place the name of all Vmware hosts except vmhost1 and vmhost2 into the variable $vmhosts)
$vmhosts = Get-VMHost | where { $_.Name -eq “vmhost1.vmlab.local” } (Search all Vmware hosts and place the host with name vmhost1 into the variable $vmhosts)
$vmhosts = Get-VMHost -location CLUSTER1 (Place all hosts that belong to the cluster called CLUSTER1 in the variable $vmhosts)
$vms = Get-VM | where { $_.NumCPU -eq “4” } (Place the names of all virtual machines with 4vCPU’s into the variable $vms)

Storage NFS

new-datastore -Nfs -VMHost ESX or ESXi Host name or IP -Name name of the datastore -NFSHost NFS Host IP -Path “Path to NFS e.g /vol/vdi-desktops” (adds a new datastore to a host)
$vmhosts | new-datastore -Nfs -VMHost ESX or ESXi Host name or IP -Name name of the datastore -NFSHost NFS Host IP -Path “Path to NFS e.g /vol/vdi-desktops” (adds a new NFS datastore to the hosts that are within the $vmhosts variable)
$vmhosts | Remove-Datastore -Datastore (Removes the NFS Datastore from all hosts within the $vmhosts variable)

Storage iSCSI or FCP LUN

$vmhosts | get-scsilun -luntype disk (List all Luns and their Multipathing Policy for all hosts within the $vmhosts variable)
$vmhosts | Get-ScsiLun -LunType disk | where {$_.MultipathPolicy -eq “RoundRobin”} (List all luns with a multipathing policy of RoundRobin for all hosts within the $vmhosts variable)
$vmhosts | Get-ScsiLun -LunType disk | where {$_.MultipathPolicy -eq “RoundRobin”} | Set-ScsiLun -MultipathPolicy MostRecentlyUsed (Searches for all Luns that have RoundRobin set as their multipathing policy and changes them to MostRecentlyUsed, for all hosts within the $vmhosts variable)


get-datastore vmware_datastore1 | get-vm (Shows which virtual machines are using the current datastore)


get-vm vmsql1 | Get-NetworkAdapter (Get the current network of vmsql1)
get-vm vmsql1 | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName HomeLabInternal (Change vmsql1 network to HomLabInternal)

HA Cluster

get-cluster vmucscluster | select HATotalSlots,HAUsedSlots,HAAvailableSlots,HASlotCpuMHz,HASlotMemoryMb,HASlotNumVCpus (Shows the same information as Advanced Runtime Info for a Cluster within the vSphere Client)
get-cluster vmucscluster | select HATotalSlots,HAUsedSlots,HAAvailableSlots,HASlotCpuMHz,HASlotMemoryMb,HASlotNumVCpus >> “c:tempvmslotinfo.txt” (Same as above, but rights the information to a file, if run multiple times, information will append within the text file)
get-cluster vmucscluster | select HATotalSlots,HAUsedSlots,HAAvailableSlots,HASlotCpuMHz,HASlotMemoryMb,HASlotNumVCpus | Out-File “c:tempvmslotinfo.txt” (Same as above, but overwrites the file if run multiple times)

Virtual Machine Hardware

set-vm -vm vmsql1 -version v9 (Sets a virtual machine hardware level to 9. Append -confirm:$false to apply changes straight away)

Virtual Machine Snapshots

get-vm | get-snapshot | select name,vm,sizemb | where { $_.Name -match “smvi” } (Search all virtual machines for snapshots containing smvi)

VMware Tools

get-vm |% { get-view $_.id } | select Name, @{ Name=”Tools Status”; Expression={$_.guest.toolsstatus}}, @{ Name=”Tools Running Status”; E={$_.guest.toolsrunningstatus}} (Retrieves the VMware tools version of each virtual machine. Combine with variables above to narrow your search. Thanks Tim for this script)

Sending Email within PowerCLI example

The following lines will do the following: Connect and send an email through the mail server The email will be from [email protected] and sent to [email protected] The subject of the email will be VMlab HA Slot Information. The script will read the contents from the txt file c:tempvmslotinfo.txt and place it in the body of the email, it will also attach the text file c:tempvmslotinfo.txt to the email as an attachment.

$smtpServer = “”
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = “[email protected]
$msg.To.Add(“[email protected]”)
$msg.Subject = “VMlab HA Slot Information”
$msg.Body = (get-content “C:tempvmslotinfo.txt” | out-string)
$file = “C:tempvmslotinfo.txt”
$att = new-object Net.Mail.Attachment($file)

Saving PowerCLI Scripts

You can place all your commands that you would normally run into a file such as powercliscript.ps1. You can run this file directly in PowerCLI.
However if you wish to schedule this script to run with task scheduler, you must place this line at the top of your script:

add-pssnapin VMware.VimAutomation.Core

This will add the Powercli Snapin for your script to run.

All the tutorials included on this site are performed in a lab environment to simulate a real world production scenario. As everything is done to provide the most accurate steps to date, we take no responsibility if you implement any of these steps in a production environment.