Power
CLI

Top 100 PowerCLI Commands

Cheat Sheet  ·  VMware vSphere / vCenter

🔌1. Connection & Session
1
Connect-VIServer -Server vcenter.domain.comConnect to vCenter Server
2
Disconnect-VIServer *Disconnect from all servers
3
$global:DefaultVIServerShow current connected server
4
Connect-VIServer -Server esxi01 -User rootConnect directly to ESXi host
5
Set-PowerCLIConfiguration -InvalidCertificateAction IgnoreIgnore SSL cert warnings
6
Set-PowerCLIConfiguration -DefaultVIServerMode MultipleAllow multiple server connections
7
Get-PowerCLIConfigurationView PowerCLI config settings
8
Connect-VIServer -Credential (Get-Credential)Connect with prompted credentials
9
$global:DefaultVIServersList all active connections
10
Disconnect-VIServer -Server vcenter -Confirm:$falseDisconnect without confirmation
🖥️2. VM Management
11
Get-VMList all virtual machines
12
Get-VM -Name "WebServer01"Get specific VM by name
13
Start-VM -VM "WebServer01"Power on a VM
14
Stop-VM -VM "WebServer01"Power off a VM (hard)
15
Shutdown-VMGuest -VM "WebServer01"Guest OS graceful shutdown
16
Restart-VMGuest -VM "WebServer01"Guest OS graceful reboot
17
Suspend-VM -VM "WebServer01"Suspend a VM
18
New-VM -Name "NewVM" -ResourcePool "Resources" -Datastore "DS01"Create a new VM
19
Remove-VM -VM "OldVM" -DeletePermanentlyDelete VM and all files
20
Set-VM -VM "WebServer01" -NumCpu 4 -MemoryGB 8Resize VM CPU and RAM
🗄️3. ESXi Host Management
21
Get-VMHostList all ESXi hosts
22
Get-VMHost -Name "esxi01.domain.com"Get specific ESXi host
23
Set-VMHost -VMHost "esxi01" -State MaintenancePut host in maintenance mode
24
Set-VMHost -VMHost "esxi01" -State ConnectedExit maintenance mode
25
Restart-VMHost -VMHost "esxi01" -Confirm:$falseReboot an ESXi host
26
Get-VMHost | Get-VMHostServiceList ESXi host services
27
Get-VMHostService | Where-Object {$_.Key -eq "ssh"} | Start-VMHostServiceEnable SSH on host
28
Get-VMHost | Get-VMHostNtpServerGet NTP server settings
29
Add-VMHostNtpServer -VMHost "esxi01" -NtpServer "pool.ntp.org"Add NTP server to host
30
Get-VMHost | Select Name, Version, Build, ConnectionStateGet host version info
🔀4. Cluster & DRS
31
Get-ClusterList all clusters
32
Get-Cluster -Name "ProdCluster"Get specific cluster
33
New-Cluster -Name "NewCluster" -Location (Get-Datacenter "DC01")Create a new cluster
34
Set-Cluster -Cluster "ProdCluster" -DrsEnabled $true -DrsAutomationLevel FullyAutomatedEnable DRS on cluster
35
Set-Cluster -Cluster "ProdCluster" -HAEnabled $trueEnable HA on cluster
36
Get-DrsRule -Cluster "ProdCluster"List DRS rules
37
New-DrsRule -Cluster "ProdCluster" -Name "KeepTogether" -KeepTogether $true -VM (Get-VM vm1,vm2)Create VM affinity rule
38
New-DrsVMHostRule -Cluster "ProdCluster" -Name "PinToHost" -VMGroup $vmg -VMHostGroup $hg -Type MustRunOnCreate VM-to-host rule
39
Get-DrsVMGroup -Cluster "ProdCluster"List DRS VM groups
40
Move-VM -VM "WebServer01" -Destination (Get-VMHost "esxi02")vMotion VM to another host
📸5. Snapshots
41
New-Snapshot -VM "WebServer01" -Name "Pre-Patch"Create a snapshot
42
New-Snapshot -VM "WebServer01" -Name "WithMemory" -Memory $trueSnapshot with memory state
43
Get-Snapshot -VM "WebServer01"List snapshots for a VM
44
Set-VM -VM "WebServer01" -Snapshot (Get-Snapshot -VM "WebServer01" -Name "Pre-Patch")Revert to a snapshot
45
Remove-Snapshot -Snapshot (Get-Snapshot -VM "WebServer01" -Name "Pre-Patch")Delete a snapshot
46
Get-VM | Get-Snapshot | Where-Object {$_.Created -lt (Get-Date).AddDays(-7)}Find snapshots older than 7 days
47
Get-VM | Get-Snapshot | Remove-Snapshot -Confirm:$falseRemove all snapshots (all VMs)
48
Get-Snapshot -VM "WebServer01" | Select Name, Created, SizeGBShow snapshot size and age
49
New-Snapshot -VM (Get-VM) -Name "Bulk-Snapshot" -RunAsyncSnapshot all VMs asynchronously
50
Get-VM | Get-Snapshot | Measure-Object SizeGB -SumTotal snapshot storage usage
💾6. Datastores & Storage
51
Get-DatastoreList all datastores
52
Get-Datastore | Select Name, FreeSpaceGB, CapacityGBShow datastore capacity
53
Get-Datastore -Name "DS01" | Get-VMList VMs on a datastore
54
Move-VM -VM "WebServer01" -Datastore "DS02"Storage vMotion a VM
55
New-Datastore -VMHost "esxi01" -Name "NFS01" -Nfs -NfsHost "nas01" -Path "/vol/nfs01"Mount NFS datastore
56
Get-VMHost | Get-ScsiLunList SCSI / FC LUNs on hosts
57
Get-Datastore | Where-Object {($_.FreeSpaceGB / $_.CapacityGB) -lt 0.10}Find datastores below 10% free
58
Get-HardDisk -VM "WebServer01"List virtual disks on VM
59
New-HardDisk -VM "WebServer01" -CapacityGB 100 -StorageFormat ThinAdd thin-provisioned disk to VM
60
Set-HardDisk -HardDisk (Get-HardDisk -VM "WebServer01" | Select -Last 1) -CapacityGB 200Expand a virtual disk
🌐7. Networking
61
Get-VirtualSwitchList all vSwitches
62
New-VirtualSwitch -VMHost "esxi01" -Name "vSwitch1"Create a standard vSwitch
63
Get-VirtualPortGroupList all port groups
64
New-VirtualPortGroup -VirtualSwitch "vSwitch1" -Name "VLAN100" -VLanId 100Create port group with VLAN
65
Get-VDSwitchList distributed vSwitches
66
Get-VDPortgroupList distributed port groups
67
Get-NetworkAdapter -VM "WebServer01"List VM network adapters
68
Set-NetworkAdapter -NetworkAdapter (Get-NetworkAdapter -VM "WebServer01") -Portgroup "VLAN100" -Confirm:$falseMove VM NIC to port group
69
Get-VMHostNetworkAdapterList host physical NICs (vmnic)
70
Get-VMHostNetworkAdapter -VMKernelList VMkernel adapters (vmk)
🛠️8. VMware Tools & Guest
71
Get-VM | Select Name, @{N="ToolsStatus";E={$_.ExtensionData.Guest.ToolsStatus}}Get VMware Tools status for all VMs
72
Update-Tools -VM "WebServer01"Update VMware Tools on VM
73
Get-VM | Where-Object {$_.ExtensionData.Guest.ToolsVersionStatus -eq "guestToolsNeedUpgrade"}Find VMs needing Tools upgrade
74
Invoke-VMScript -VM "WebServer01" -ScriptText "ipconfig /all" -GuestCredential $credRun script inside guest OS
75
Copy-VMGuestFile -Source "C:\log.txt" -Destination "\\server\share\" -VM "WebServer01" -GuestToLocal -GuestCredential $credCopy file from guest to host
76
Get-VM | Select Name, Guest, @{N="IP";E={$_.Guest.IPAddress[0]}}Get IP addresses of all VMs
77
Get-VM | Select Name, @{N="OS";E={$_.ExtensionData.Config.GuestFullName}}Get guest OS type for all VMs
78
Set-VM -VM "WebServer01" -GuestId "windows2019srv_64Guest"Change VM guest OS type
79
Get-VMGuest -VM "WebServer01"Get detailed guest info
80
Mount-Tools -VM "WebServer01"Mount VMware Tools ISO in guest
📊9. Reporting & Inventory
81
Get-VM | Select Name, PowerState, NumCpu, MemoryGB, VMHost | Export-Csv "vms.csv" -NoTypeInformationExport VM inventory to CSV
82
Get-VMHost | Select Name, Version, Build, @{N="CPUUsage%";E={[math]::Round($_.CpuUsageMhz/$_.CpuTotalMhz*100,1)}}Host CPU utilization report
83
Get-VM | Where-Object {$_.PowerState -eq "PoweredOff"}List all powered-off VMs
84
Get-VM | Sort-Object MemoryGB -Descending | Select -First 10Top 10 VMs by memory
85
Get-DatacenterList all datacenters
86
Get-FolderList all inventory folders
87
Get-ResourcePoolList all resource pools
88
Get-Alarm -Entity (Get-Cluster "ProdCluster")Get alarms on a cluster
89
Get-VM | Get-AnnotationGet custom VM attributes
90
Get-VM | Select Name,@{N="vCPU";E={$_.NumCpu}},@{N="RAM_GB";E={$_.MemoryGB}},@{N="Host";E={$_.VMHost}},@{N="DS";E={$_.DatastoreIdList}} | Export-Csv "full_inv.csv" -NoTypeInformationFull inventory export to CSV
</>10. Advanced & Automation
91
Get-VM | ForEach-Object { Start-VM $_ -RunAsync }Power on all VMs async
92
Get-Task | Where-Object {$_.State -eq "Running"}List all running vCenter tasks
93
Get-VIEvent -MaxSamples 100 -Type VmPoweredOnEventGet recent power-on events
94
Get-VIEvent -Entity "WebServer01" -MaxSamples 50Get events for specific VM
95
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec; $spec.annotation = "Owner: IT"; (Get-VM "WebServer01").ExtensionData.ReconfigVM($spec)Set VM annotation via .NET API
96
Get-VMHostAdvancedConfiguration -VMHost "esxi01" -Name "Syslog.global.logDir"Get advanced host config value
97
Set-VMHostAdvancedConfiguration -VMHost "esxi01" -Name "Syslog.global.logHost" -Value "udp://syslog:514"Set syslog server on host
98
Get-VMHost | Get-VMHostFirewallException | Where-Object {$_.Enabled -eq $true}List enabled firewall rules on hosts
99
New-TagCategory -Name "Environment" -Cardinality Single; New-Tag -Name "Production" -Category "Environment"Create tag category and tag
100
Get-VM "WebServer01" | New-TagAssignment -Tag (Get-Tag "Production")Assign tag to a VM
💡 Tips for Using PowerCLI
Install via: Install-Module VMware.PowerCLI -Scope CurrentUser
Always Connect-VIServer before running any cmdlets.
Use -RunAsync on long operations to avoid blocking the shell.
Use Get-Help Get-VM -Examples for built-in cmdlet examples.
Pipe | Export-Csv to save any report for Excel review.
Use .ExtensionData to access raw vSphere API objects.
Example Pipelines
# Find all VMs with snapshots older than 7 days and remove them
Get-VM | Get-Snapshot | Where-Object {$_.Created -lt (Get-Date).AddDays(-7)} | Remove-Snapshot -Confirm:$false
# vMotion all VMs off a host before maintenance
Get-VMHost "esxi01" | Get-VM | Move-VM -Destination (Get-VMHost "esxi02") -RunAsync
# Report datastores under 10% free space
Get-Datastore | Where-Object {$_.FreeSpaceGB / $_.CapacityGB -lt 0.10} | Select Name, FreeSpaceGB, CapacityGB
# Bulk-enable SSH on all hosts in a cluster
Get-Cluster "ProdCluster" | Get-VMHost | Get-VMHostService | Where-Object {$_.Key -eq "ssh"} | Start-VMHostService