Managing VMware with PowerCLI: Essential PowerShell Commands

Introduction VMware PowerCLI is a powerful tool that allows administrators to manage VMware environments using PowerShell. Whether you need to create virtual machines, check resource usage, or troubleshoot storage capacity, PowerCLI provides a streamlined approach to VMware management. Below is a guide to setting up PowerCLI and using essential commands for day-to-day VMware administration.


Step 1: Install and Import VMware PowerCLI

Before running VMware-related PowerShell commands, ensure that VMware PowerCLI is installed on your system.

Install-Module -Name VMware.PowerCLI -Scope CurrentUser -Force

After installation, import the module:

Import-Module VMware.PowerCLI

If you encounter SSL/TLS certificate warnings while connecting, configure PowerCLI to ignore invalid certificates:

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

Step 2: Connect to vCenter Server

To manage your VMware environment, you need to authenticate with vCenter:

Connect-VIServer -Server <Your-VCenter-Server> -User <Your-Admin-User> -Password '<Your-Password>'

Once connected, you can retrieve information about your virtual infrastructure.


Step 3: List VMware Hosts

To view all available VMware hosts:

Get-VMHost

This provides a list of all ESXi hosts, their connection status, and available resources.


Step 4: Retrieve Virtual Machines

To get a list of all VMs in the environment:

Get-VM

For details of a specific VM:

Get-VM -Name <VM-Name>

Step 5: Checking vSAN Datastore Usage

One common challenge in VMware environments is monitoring vSAN datastore usage. To check storage space:

Get-VsanSpaceUsage -Cluster <Your-Cluster-Name>

To calculate the percentage of used space, run:

$vsanUsage = Get-VsanSpaceUsage -Cluster <Your-Cluster-Name>
$usedPercentage = 100 - (($vsanUsage.FreeSpaceGB / $vsanUsage.CapacityGB) * 100)
"vSAN Datastore is currently {0:N2}% full" -f $usedPercentage

Step 6: Creating a New Virtual Machine

If you need to create a new VM:

New-VM -Name <VM-Name> -VMHost <Host-Name> -Datastore <Datastore-Name> -DiskGB 50 -MemoryGB 4 -NumCPU 2

This command creates a VM with 50GB disk, 4GB RAM, and 2 CPUs.


Step 7: Cloning an Existing Virtual Machine

To create a clone of an existing VM:

New-VM -Name <New-VM-Name> -VM <Source-VM-Name> -Datastore <Datastore-Name> -VMHost <Target-Host>

Step 8: Managing VM Power States

To power on a VM:

Start-VM -VM <VM-Name>

To shut down a VM:

Stop-VM -VM <VM-Name> -Confirm:$false

To restart a VM:

Restart-VM -VM <VM-Name> -Confirm:$false

Step 9: Deleting a Virtual Machine

If a VM is no longer needed, you can remove it permanently:

Remove-VM -VM <VM-Name> -DeletePermanently -Confirm:$false

Step 10: Checking for Leftover Files in vSAN

Even after deleting a VM, some files may remain in the datastore. You can check for orphaned files:

Get-Datastore -Name <Datastore-Name> | Get-ChildItem -Recurse | Where-Object { $_.Name -like "*<VM-Name>*" }

To manually remove leftover files:

Remove-Item -Path "vmstore:\<Datastore-Name>\FolderName\<VM-Name>.vmdk" -Confirm:$false

Final Thoughts

Using PowerCLI to manage VMware environments improves efficiency and automation. Whether you need to monitor vSAN usage, create new VMs, or automate backups, PowerCLI provides a flexible solution. Keep this guide handy for reference as you work with VMware environments.

Have any useful PowerCLI commands that you frequently use? Share them in the comments below! 🚀

© 2012–2025 Jet Mariano. All rights reserved.
For usage terms, please see the Legal Disclaimer.

error: Content is protected !!