Starter Guide | VMware PowerCLI Commands Install & Examples
If you want an easier way to manage VMs on vSphere, VMware PowerCLI is a powerful command-line tool you can use. In this article, I will introduce what is it and the basic VMware PowerCLI commands.
What is VMware PowerCLI
VMware PowerCLI is a powerful command line interface (CLI) tool that is distributed as a Windows PowerShell module. It provides cmdlets for managing and automating automating vSphere, VMware Site Recovery Manager, vSphere Automation SDK, vCloud Director, vSphere Update Manager, etc.
Simply put, it can save you a lot of time by automating vSphere routine tasks and repetitive operations, for example, exporting VM from ESXi, 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 basic VMware PowerCLI commands, and demonstrate how to use basic cmdlets to automate the creation of multiple VMs.
How to install and use PowerCLI on VMware
Same as installing Hyper-V PowerShell module, you need to install and update VMware PowerCLI module as well before running PowerCLI commands for VMware.
Now I will give an example to install and access PowerCLI based on Windows 10.
✦ To install VMware PowerCLI, just open Windows PowerShell as administrator, and run the following command:
Install-Module -Name VMware.PowerCLI
Note: Using "-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.
If you cannot download PowerCLI module from Microsoft or you want to install PowerCLI offline, you can optionally download the PowerCLI ZIP file from the home page and transfer it to your local machine:
- Check the PowerShell Module path by this command: $env:PSModulePath
- Extract the contents of the ZIP file to one of the listed folders.
- Unblock the files by: cd and Get-ChildItem * -Recurse | Unblock-File
✦ To verify if the PowerCLI module is available now, use the command:
Get-Module -Name VMware.PowerCLI* -ListAvailable
✦ 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]
11 basic VMware PowerCLI commands
With the VMware PowerShell module installed, now you can use VMware vSphere PowerCLI cmdlets to manage your host and VMs.
In this section, I will introduce 11 basic PowerCLI commands for VMware.
1. 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]
2. 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
3. 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
4. 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]
5. 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 vSphere 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]
6. 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])
7. 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]
8. -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]
9. 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
10. 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*
11. 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 for VMware.
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.
Use PowerCLI 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 vSphere 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}
You can then start the VM by running the command:
Start-VM [vmname]
Easier way to create new identical 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 AOMEI Cyber Backup.
In practical use, you can configure a scheduled task to create virtual machines backups automatically and easily for both paid and free versions of VMware ESXi or Hyper-V, thus when needed, you can quickly restore the VM in place, or create a new VM to new location from any history backup version.
Here I will give an example to show you how to create a VM backup and create a new VM from it:
1. Download this free trial and install it. You can then click add Hyper-V, vCenter or Standalone ESXi host as the source device.
*You can choose to install this VM backup software on either Windows or Linux system.
2. Create backup tasks: enter task name >> choose device and virtual machines >> schedule the task >> set backup cleanup policy. Click Start backup to create and execute the task.
3. Then you can Restore from any history backups and choose Restore to new location. It creates identical new VM from the backup without any complicated configuration. You can also customize the RAM and Virtual Processor or the new VM.
In this way, you are able to create identical 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.