skip to content

Search

Syspirit
EN

PowerCLI

Essential PowerCLI commands & practical examples to manage VMware environments via PowerShell!

VMware PowerCLI is the PowerShell command-line interface for administering vSphere, vCenter, and VMware Cloud environments.

Installation

# Install PowerCLI from PowerShell Gallery
Install-Module -Name VMware.PowerCLI -Scope CurrentUser
 
# Verify installation
Get-Module -Name VMware.PowerCLI -ListAvailable

Connection and configuration

ActionCommand
Connect to vCenter/ESXiConnect-VIServer server-01
Connect with credentialsConnect-VIServer server-01 -User admin -Password pass
List active connections$global:DefaultVIServers
Disconnect from allDisconnect-VIServer * -Confirm:$false
Ignore SSL certificatesSet-PowerCLIConfiguration -InvalidCertificateAction Ignore

VM Management

ActionCommand
List all VMsGet-VM
Get a specific VMGet-VM "vm-name"
Start a VMStart-VM "vm-name"
Graceful shutdown (with VMware Tools)Stop-VMGuest "vm-name"
Forced shutdown (power button)Stop-VM "vm-name" -Confirm:$false
Graceful restartRestart-VMGuest "vm-name"
Suspend a VMSuspend-VM "vm-name"

VM Configuration

ActionCommand
Modify CPU and RAMSet-VM "vm-name" -NumCpu 4 -MemoryGB 8
Add a hard diskNew-HardDisk -VM "vm-name" -CapacityGB 50
Add a network adapterNew-NetworkAdapter -VM "vm-name" -NetworkName "VLAN-100"
Modify notesSet-VM "vm-name" -Notes "Description"
Create a new VMNew-VM -Name "new-vm" -VMHost server-01 -Datastore DS1

ESXi Host Management

ActionCommand
List all ESXi hostsGet-VMHost
Specific host informationGet-VMHost server-01
Enter maintenance modeSet-VMHost server-01 -State Maintenance
Exit maintenance modeSet-VMHost server-01 -State Connected
Restart an ESXi hostRestart-VMHost server-01
VMs on a specific hostGet-VMHost server-01 | Get-VM

Datastore Management

ActionCommand
List all datastoresGet-Datastore
Datastore informationGet-Datastore "DS-PROD"
Available disk spaceGet-Datastore | Select Name, CapacityGB, FreeSpaceGB
VMs on a datastoreGet-Datastore "DS-PROD" | Get-VM

Snapshots

ActionCommand
Create a snapshotNew-Snapshot -VM "vm-name" -Name "snapshot-name"
List snapshotsGet-VM "vm-name" | Get-Snapshot
Restore a snapshotSet-VM "vm-name" -Snapshot "snapshot-name"
Delete a snapshotRemove-Snapshot -Snapshot "snapshot-name"
Delete all snapshotsGet-VM "vm-name" | Get-Snapshot | Remove-Snapshot

Migrations (vMotion)

ActionCommand
vMotion to another hostMove-VM -VM "vm-name" -Destination server-01
Storage vMotionMove-VM -VM "vm-name" -Datastore "DS-PROD"
Full migrationMove-VM -VM "vm-name" -Destination server-01 -Datastore DS1

Practical examples

First connection and configuration

# Connect to vCenter and ignore certificates
Connect-VIServer server-01
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

Quick resource inventory

# Environment overview
Write-Host "=== VMWARE INVENTORY ==="
Write-Host "ESXi Hosts: $(Get-VMHost | Measure-Object | Select -ExpandProperty Count)"
Write-Host "Total VMs: $(Get-VM | Measure-Object | Select -ExpandProperty Count)"
Write-Host "Powered On VMs: $(Get-VM | Where {$_.PowerState -eq 'PoweredOn'} | Measure | Select -ExpandProperty Count)"

Complete VM creation

# Create a new VM with configuration
$VMParams = @{
    Name = "WEB-SERVER-01"
    VMHost = "server-01"
    Datastore = "DS-PROD"
    NumCpu = 2
    MemoryGB = 4
    DiskGB = 40
    NetworkName = "VLAN-100"
}
New-VM @VMParams

Host maintenance mode

# Put a host in maintenance (automatically migrates VMs)
Write-Host "Migrating VMs..."
Set-VMHost server-01 -State Maintenance -VsanDataMigrationMode EnsureAccessibility

Snapshot before maintenance

# Create snapshots for all VMs on a host
Get-VMHost server-01 | Get-VM | Where {$_.PowerState -eq "PoweredOn"} |
    New-Snapshot -Name "Before-Maintenance-$(Get-Date -Format 'yyyyMMdd')" -Memory:$false

Resource usage report

# Generate a CSV report of VMs
Get-VM | Select Name, PowerState, NumCpu, MemoryGB,
    @{N="UsedSpaceGB";E={[math]::Round($_.UsedSpaceGB,2)}},
    @{N="VMHost";E={$_.VMHost.Name}} |
    Export-Csv "VM-Report.csv" -NoTypeInformation