If you are managing a VMware environment, doing everything through the vSphere client GUI can quickly become tedious. Enter PowerCLI, VMware’s powerful PowerShell module that allows you to automate almost any vSphere task.
Whether you are spinning up a new automation pipeline or just need to quickly manage snapshots across your environment, having a few reliable go-to commands saves an immense amount of time. Below is a quick-start cheat sheet for installing PowerCLI, connecting to your vCenter, and managing VM snapshots.
# Install the VMware PowerCLI module for the current user
Install-Module VMware.PowerCLI -Scope CurrentUser
# Bypass untrusted SSL certificate warnings and connect to vCenter
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Connect-VIServer vcenter.domain.local
# List all virtual machines on this vCenter
Get-VM
# Create a new snapshot for a specific VM
Get-VM "servername" | New-Snapshot -Name "test snapshot" -Description "testing, hope it works"
# View all snapshots associated with a specific VM
Get-VM "servername" | Get-Snapshot
# Delete all snapshots for a specific VM (without prompting for confirmation)
Get-VM "servername" | Get-Snapshot | Remove-Snapshot -Confirm:$false
# List only the names of the snapshots on a specific VM
Get-VM "servername" | Get-Snapshot | Select-Object Name
# Find and delete a specific snapshot by its name
Get-VM "servername" | Get-Snapshot -Name "test snapshot" | Remove-Snapshot -Confirm:$false
What’s Happening Under the Hood?
- Installation & Connection: The script starts by downloading PowerCLI directly from the PowerShell Gallery. It then suppresses self-signed certificate warnings, common in lab environments, before establishing a connection to the vCenter server.
- The Power of the Pipeline: By using PowerShell’s pipeline (
|), we can seamlessly pass a virtual machine object from Get-VM straight into snapshot management commands like New-Snapshot or Remove-Snapshot. - Automation-Friendly: By appending -Confirm:$false, the script bypasses interactive “Are you sure?” prompts, making these snippets perfect for embedding into larger, unattended automation scripts.
