Virtualization2 min read193 lines

Learning state

Track this guide

Saved in this browser only. No account required.

VMware PowerCLI Master Class

Engineering-grade reference manual for VMware vSphere operations with PowerCLI: connection context, inventory, VM lifecycle, networking, datastores, snapshots, templates, and evidence collection.

Overview

PowerCLI gives infrastructure teams repeatable vSphere administration from PowerShell. Use it to inventory vCenter, clusters, hosts, datastores, networks, VMs, snapshots, and templates. Treat power changes, snapshots, migrations, and deletion as approval-gated operations.

Connection Context

Install PowerCLI:

Install-Module VMware.PowerCLI -Scope CurrentUser

Connect to vCenter:

Connect-VIServer -Server VCENTER_FQDN

Show connected servers:

$global:DefaultVIServers

Disconnect when finished:

Disconnect-VIServer -Server VCENTER_FQDN -Confirm:$false

Inventory

List datacenters:

Get-Datacenter

List clusters:

Get-Cluster

List hosts:

Get-VMHost

List VMs:

Get-VM

Show VM details:

Get-VM -Name VM_NAME | Select-Object Name,PowerState,NumCpu,MemoryGB,VMHost

VM Lifecycle

Start a VM after approval:

Start-VM -VM VM_NAME

Gracefully shut down a VM after approval:

Shutdown-VMGuest -VM VM_NAME -Confirm:$false

Stop a VM only after explicit approval:

Stop-VM -VM VM_NAME -Confirm:$false

Restart VMware Tools inside the guest after approval:

Restart-VMGuest -VM VM_NAME -Confirm:$false

Snapshots

List snapshots:

Get-VM -Name VM_NAME | Get-Snapshot

Create a snapshot after approval:

New-Snapshot -VM VM_NAME -Name "Change Window" -Description "Pre-change checkpoint"

Remove a snapshot after approval:

Get-VM -Name VM_NAME | Get-Snapshot -Name SNAPSHOT_NAME | Remove-Snapshot -Confirm:$false

Find old snapshots:

Get-VM | Get-Snapshot | Where-Object Created -lt (Get-Date).AddDays(-7)

Datastores and Capacity

List datastores:

Get-Datastore | Select-Object Name,FreeSpaceGB,CapacityGB

Find low-capacity datastores:

Get-Datastore | Where-Object {$_.FreeSpaceGB / $_.CapacityGB -lt 0.15}

List virtual disks:

Get-VM -Name VM_NAME | Get-HardDisk

Networking

List virtual switches:

Get-VirtualSwitch

List port groups:

Get-VirtualPortGroup

Show VM network adapters:

Get-VM -Name VM_NAME | Get-NetworkAdapter

Change a network adapter after approval:

Get-VM -Name VM_NAME | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName PORTGROUP_NAME -Confirm:$false

Templates and Clones

List templates:

Get-Template

Create a VM from a template after approval:

New-VM -Name NEW_VM_NAME -Template TEMPLATE_NAME -Datastore DATASTORE_NAME -VMHost HOST_NAME

Convert a VM to template after approval:

Set-VM -VM VM_NAME -ToTemplate -Confirm:$false

Evidence Checklist

  • Connected vCenter is correct.
  • VM power state and host placement match expectation.
  • Datastore free space is sufficient.
  • Snapshots are current, intentional, and not stale.
  • Network adapters point to expected port groups.
  • Any lifecycle or storage mutation has approval and rollback notes.