VMware PowerCLI

Cheat Sheet for PowerCLI

Show all Snapshots older than 14 days
Get-VM |Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-14)} |Select VM,Created,Name,SizeMB |FT -autosize
Remove all Snapshots older than 14 days
Get-VM |Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-14)} | Remove-Snapshot -Confirm:$false
Show all powered off
VMs Get-VM |Where-object {$_.powerstate -eq "poweredoff"} | FT -autosize
Show Guest OS of all (running) VMs
Get-VM |Where-object {$_.powerstate -eq “poweredon"} | Get-VMGuest | FT -autosize
Show VMDK size of all VMs
Get-VM | Select-Object Name,@{n="HardDiskSizeGB"; e={(Get-HardDisk -VM $_ | Measure-Object -Sum CapacityGB).Sum}}
List all virtual disks pointing to an RDM device (and ignore errors)
Get-Datastore -WarningAction 0 | Get-HardDisk -DiskType "RawPhysical","RawVirtual" | Select "Filename","CapacityKB" | fl
Remove all CD/ISO from VMs
Get-VM | Get-CDDrive |Remove-CDDrive -Confirm:$false
List all storage devices on specific host
Get-ScsiLun -VMhost 10.0.0.100 -LunType disk |Select CanonicalName,CapacityGB,MultipathPolicy |FT -autosize
List all storage devices on all hosts
Get-VMHost |Get-ScsiLun -CanonicalName "*" -LunType disk |Select CanonicalName,CapacityGB,MultipathPolicy |FT -autosize
List all paths to storage devices on specific host
Get-ScsiLun -VMhost 10.0.0.100 -LunType disk |Get-ScsiLunPath |Select Name,SanID,State |FT -autosize
Remove all Snapshots for all VMs in Folder ‘Test'
Create Snapshot for all VMs in Folder ‘Test' Get-Folder 'Test' | Get-VM | New-Snapshot -Name AutoSnapshot
Migrate VM to other host or datastore
Get-VM VM1 | Move-VM -Destination (Get-VMHost ESXHost2)
Get-VM VM1 | Move-VM -Datastore DS
List all dvSwitch Ports
Get-VMHost |Get-VDSwitch |Get-VDPort |FT -autosize

Last updated