By Alison / Last Updated April 24, 2022

What is VMware PowerCLI

VMware PowerCLI is a powerful command line interface (CLI) tool that is distributed as a Windows PowerShell module.

It can save you a lot of time by automating vSphere routine tasks and repetitive operations, especially when you are managing a large number of hosts or VMs. With more than 800 cmdlets PowerCLI provided, you can automate all aspects of vSphere management, including networking, storage, VMs, tasks and more.

In this article, I will introduce 10 basic VMware PowerCLI commands, and demonstrate how to use basic cmdlets to automate the creation of multiple VMs.

VMware Logo

How to access to PowerCLI on VMware

To run PowerCLI commands in VMware, you need to start with installing and updating the VMware PowerCLI module.

✦ To install VMware PowerCLI, just open Windows PowerShell as administrator, and run the following command:

Install-Module -Name VMware.PowerCLI

Note: Using the -Scope CurrentUser parameter allows Powershell to install the module without administrator access. The Syntax is:
Install-Module -Name VMware.PowerCLI -Scope CurrentUser

If you see a warning informing that “you are installing the modules from an untrusted repository”, type Y and press Enter to continue.

Untrusted Repository

✦ To check the VMware PowerCLI module version, run the command:

Find-Module -Name VMware.PowerCLI

✦ To update to a new version of VMware PowerCLI, run the command:

Update-Module -Name VMware.PowerCLI

Note: Using Update-Module, the existing version of the module will not be removed. You can uninstall the existing version of the module and then install the new version.

✦ To uninstall the existing version of VMware PowerCLI module, run the command:

Get-Module -Name VMware.Module | Uninstall-Module -Force

✦ To uninstall a specific version of VMware PowerCLI module, run the command:

Uninstall-Module -Name VMware.PowerCLI -RequiredVersion [version number]

Uninstall specific version of PowerCLI module

10 Basic VMware PowerCLI commands

With the VMware PowerShell module installed, now you can use VMware PowerCLI commands to manage your vSphere host and VMs.

In this section, I will introduce 10 basic VMware PowerCLI commands.

Connect-VIServer: How to connect to vCenter

To manage VMware vSphere, you need to connect to your vCenter server first, run the command:

Connect-VIServer -Server [server name] -Protocol [http] -User [user name] -Password [password]

Get-VMHost: How to retrieve host information

Get-VMHost is used to retrieve your host information. To view all hosts on the vCenter Server you connected, run the command:

Get-VMHost

Get-VM: How to retrieve VM information

Get-VM is the most basic cmdlet to retrieve VM information. To list all VMs on your connected vCenter and their basic information, run the command:

Get-VM

As you can see, this command only retrieves Name, PowerState, NumCPUs, and MemoryGB properties by default.

To view more information of a specific VM on the vCenter, run the command:

Get-VM -Name [vmname] | Format-List

Get-VM Format-List

Start-VM & Stop-VM: How to start and stop VM

To start a specific VM on connected vCenter, run the command:

Start-VM ‑VM [vmname]

To power off the VM, run the command:

Stop-VM ‑VM [vmname]

New-VM: How to create new VM

New-VM cmdlet is used to create new VM on the connected server.

Using the following command, you can create a new blank VM with default settings, which you need to deploy the OS later:

New-VM -Name [vmname] -VMHost [hostname]

Except for creating new VMs, you can also use VMware PowerCLI commands to create new datacenter or new folder.

To create a new folder, the basic syntax is:

New-Folder -Name [folder name]

To get the inventory root folder and create a new folder in it.

$folder = Get-Folder -NoRecursion | New-Folder -Name [folder name]

To create a new datacenter in the folder that is specified by the -Location parameter:

New-Datacenter -Location $folder -Name [datacenter name]

Move-VM: How to move VM to another location

Move-VM command allows you to move single or multiple VMs from one host or datastore to another.

To live migrate a single VM to another host, run the command:

Move-VM -VM [vmname] -Destination [hostname]

To live migrate a single VM to another datastore, run the command:

Move-VM -VM [vmname] -Datastore [datastore name]

To move all VMs from one host to another host, run the command:

Get-VMHost [hostname] | Get-VM | Move-VM -Destination (Get-VMHost [hostname])

New-Snapshot: How to take VM snapshot

VMware snapshot is a convenient quick failsafe measure for VMs. There are also many VMware PowerCLI commands that can help you to quickly create and manage your VMware snapshots.

To create a new snapshot of the specified VM, the basic syntax is:

New-Snapshot -VM [vmname] -Name [snapshot name]

To create a new snapshot of the powered-on VM and preserves its memory state:

New-Snapshot ‑VM [vmname] ‑Name [snapshot name] -Description [description] ‑Memory $true

To view all the snapshot of the specified VM:

Get-Snapshot -VM [vmname]

Get-Snapshot

-ToTemplate: How to convert VM to template

Create multiple VMs from a template is a common operation using VMware PowerCLI. Before you can do so, you need an available template. You can use ToTemplate command to convert VM to template

To convert a VM to template, run the command:

Set-VM -ToTemplate -Confirm:$false

To retrieve all VM templates in the specified datacenter:

Get-Template -Location [datacenter]

Invoke-VMScript: How to run script easier

If your network connection to a VM is lost, or if you need to automate tasks on single or more VMs, Invoke-VMScript can send commands directly to the VM without the need for a normal WinRM or SSH connectivity. For example:

Runs a PowerShell script. In PowerShell, to access environment variables, you must use the following syntax: $env: Also, to run the program, you must specify an ampersand (&) in front of the program path. The outer quotes ($script = '...') are required because this is how you define a string variable in PowerShell. The inner double quotes are required because there are spaces in the path.

$script = '&"$env:ProgramFiles\Common Files\Microsoft Shared\MSInfo\msinfo32.exe" /report "$env:Tmp\inforeport"'
Invoke-VMScript -ScriptText $script -VM VM -GuestCredential $guestCredential

Get-VICommand: How to view all available commands

This function retrieves all commands of the imported VMware modules, including cmdlets, aliases, and functions.

If you are not sure what VMware PowerCLI command should be used to accomplish your work, Get-Command is a convenient way to retrieve all relevant commands of modules, including cmdlets, aliases, and functions:

To retrieve all available commands of the imported VMware modules:

Get-VICommand

To retrieve all relevant commands for the specified thing, for example, snapshot:

Get-VICommand *snapshot*

Get-VICommand *snapshot*

Get-Help: How to access official help system

If you are not familiar with VMware PowerCLI, there is also a built-in help system that allows you to quickly retrieve the usage of PowerCLI commands.

Run the following command to display the basic information of the command, such as synopsis, syntax, and descriptions:

Get-Help [command]

If you want to view all of the help information about this command, including parameters and examples, run the command:

Get-Help [command] -Full

Note: Before you start, you may need to run Update-Help to install the most current help files for Windows PowerShell modules. If you haven't updated it in a while, you may be prompted to update it when you use Get-Help. Type Y and press Enter to continue, and then you will be able to make a query again.

Update Get-Help

Using PowerCLI commands to create single or multiple VMs from template

With VMware PowerCLI, you can automate various administration tasks on single or multiple VMs. In last section, I’ve introduced several basic VMware PowerCLI commands, what you need to do later is to wrap them up. Here I will demonstrate how to quickly create multiple VMs from a template as an example.

To convert a template to a VM, the basic syntax is:

Set-Template -Template [template name] -ToVM

To create one new VM from template, run the command:

New-VM -Name [vmname] -Template [template name] -Datastore [datastore name] -ResourcePool [resourcepool name] -Location [folder name] -OSCustomizationSpec [cust name]

To create 10 VMs from a template, run the following commands:

1..10 | Foreach {$Num = “{0:00}” -f $_
New-VM -Name [vmname] “$Num” -Template [template name] -Datastore [datastore name] -ResourcePool [cluster name] -Location [folder name] -RunAsync}

Create multiple new VM from template

You can then start the VM by running the command:

Start-VM [vmname]

Simpler way to create multiple VMs from backup

When exporting VMs to OVF templates using PowerCLI, you can use it as a backup method to create multiple VMs. But for enterprise level backups, we suggest you to use the professional backup software such as AOMEI Cyber Backup. It is easier and secure to create multiple virtual machines backups for VMware ESXi or Hyper-V, and it also provides fast recovery.

1. Download AOMEI Cyber Backup and Add Devices to it.

Download Free TrialVMware ESXi 6.0 & later versions
Secure Download

2. Create backup tasks: enter task name >> choose device and virtual machines >> schedule the task >> select retention policy.

create backup

3. Click Start backup.

You are able to create multiple virtual machines conveniently in original or new virtual environment.

Summary

VMware PowerCLI is such a deep area that one article cannot cover it thoroughly. Except for the basic commands I introduced, there are so many other VMware PowerCLI commands you can use to automate complex tasks on vSphere.

But PowerCLI is not suitable for efficient enterprise backups, so if you need to back up VMware ESXi or Hyper-V, you need to choose a professional software to create backups.