Have you ever had to create 10, 30 or maybe 50 Virtual Machines in a short time period? Or modify a setting on all of your hosts? For example, enabling the SSH service? To automate tasks and repetitive actions, you must create your own scripts! You do not need to automate complex tasks to save time. Sometimes, automating a very simple task can help you save a lot of time. Automating VMware tasks is essential for every VMware Administrators.

Some years ago, VMware released PowerCLI. PowerCLI is probably the best tool for automating management and configuration of VMware vSphere. VMware PowerCLI is a command-line interface distributed as a Microsoft Windows PowerShell snap-in.

In this article, I will cover the basics of using VMware PowerCLI commands. Due to the large number of cmdlets (more than 600 cmdlets), I cannot cover all the PowerCLI cmdlets.

Note that you can use the PowerCLI modules to manage all supported VMware products such as:

Download Banner
  • vSphere,
  • Horizon View,
  • vCloud,
  • vRealize Operations Manager,
  • vSAN,
  • NSX-T,

You can install VMware PowerCLI using Windows PowerShell because the VMware PowerCLI modules are available on the PowerShell Gallery Website. Open the PowerShell console and run the following command:

PS > Install-Module VMware.PowerCLI –Scope CurrentUser

Note: We use the scope as Current User because it does not require admin privilege.

For those of you who need to upgrade VMware PowerCLI to a new version, you must uninstall the existing instance of PowerCLI prior to upgrading. Run the following command:

PS > Get-Module VMware.Module_Name | Uninstall-Module -Force

If the following directory still exists, then remove it:

C:\Program Files (x86)\VMware\Infrastructure\

Before going deeper, if you need help during this article, you can run the following command:


It will open up your default web browser and connect you to this link:

I recommend updating the help content to ensure you have the latest information. On a regular basis and especially after upgrading PowerCLI version, run the following command:


Below is an example to get assistance on the proper syntax of the Get-VM cmdlet:


Double click to the PowerCLI icon to start VMware PowerCLI.


This will open the Windows PowerShell console and load all VMware PowerCLI modules. You can open the ISE through the console and it will load the PowerCLI modules.


To get help about the PowerCLI modules, select the module in the right panel called ‘Commands’, and double-click the cmdlet you want.


To find all PowerShell Cmdlets with a specific word, use the Get-VICommand cmdlet:


PowerCLI environment is ready, so that we can automate configuration for vCenter activities. First, connect to your vCenter environment using the Connect-VIServer cmdlet:

Connect-VIServer -User -Password

After connecting to your vCenter server, a default variable is created and can be used to retrieve vCenter information. This variable called $DefaultVIServers, contains all connected servers for the current VMware PowerCLI session:

To start, run the Get-Datacenter cmdlet with no additional parameters to see whether there are any existing objects in vCenter. In my case, this cmdlet returns only one Datacenter called “New-York”.

Creating a new Datacenter called “Miami”, can be perform using the New-Datacenter cmdlet:


Hosts and Clusters

As you can see, the use of commands is very intuitive. At this step, a datacenter has been created, so we can create a new VMware cluster using the New-Cluster cmdlet with the following parameters:

  • HAEnabled: Indicate that VMware high availability is enabled
  • DrsEnabled: Indicate that VMware DRS (Distributed Resource Scheduler) is enabled


We will now set the HA Restart Priorities settings to High using the Set-Cluster cmdlet with the following parameter:

  • HARestartPriority: Specify the cluster HA restart priority. The valid values are Disabled, Low, Medium, and High. VMware HA is a feature that detects failed virtual machines and automatically restarts them on alternative ESX hosts

To disable the DRS feature, set the value to false:

Set-Cluster -Cluster RegionA01-COMP01 -DrsEnabled:$false -Confirm:$false

Note that you can add the “-Confirm:$false” parameter. If you do not add this parameter, your script will wait an input to confirm the action:

To manage correctly your VMware resources, it is essential to create a folder structure as you do with your fileserver. Creating a new folder can be perform using the New-Folder cmdlet on a vCenter Server:


From now on, you have a folder where you can move your Virtual Machines. To move a VM, use the Move-VM cmdlet:


We will use the “Get-VMHost” cmdlet to get information about what hosts are available in a vCenter server, datacenter, cluster, and on what host a specific virtual machine is running. You can filter the location by adding the “location” parameter.

  • In the first example, the command returns two hosts
  • In the second example, the command does not return any hosts, which is true because it doesn’t have ESXi hosts in my datacenter


The “Get-VMHost” cmdlet can also be used to list ESXi host where a specific VM is located. In this example, we want to know where the VMs called “VM01” and “VM06” are located:


Performing automatic action on your ESXi hosts must be done carefully. In this example, we place a vSphere Host into Maintenance Mode using Set-VMHost cmdlet:


Using VMware PowerCLI, we can also add a new host to a datacenter in a vCenter server:

Add-VMHost -Name Hostname -Location -User root -Password


To retrieve a list of all datastores from the vCenter server we are currently connected to, use the Get-Datastore cmdlet:


Virtual Machines

Using the New-VM cmdlet to automate Virtual Machine provisioning is very simple. One thing to be careful: In the following example, I tried to deploy a Virtual Machine on a cluster in which one of my ESXi node is not available.


To bypass this issue, you must specify the ESXi node name:


When you create a new Virtual Machine without extra parameters, the default settings are below:


Once the Virtual Machine is created, the VM state is stopped. You must start the VM using the Start-VM cmdlet:


When the VM is started, run the Open-VMConsoleWindow cmdlet to open a remote console session:



First, you must create a copy of the VM so that we can use that copy as a template. You can convert an existing VM to a template but it is more secure to use a copy.


After you deploy the clone called “FromVM07Template”, the next step is to convert this new VM into a template. Use the “ToTemplate” parameter that will convert the specified VM to a template.


Confirm if a template exists. Now we can deploy a VM from this new template using the New-VM cmdlet again followed by the “Template” parameter.


Configuring a VM

You will see that this VM only has one disk. Now you can use the Set-HardDisk cmdlet to change the disk size. When you execute the command, it will ask you for a confirmation, or you can add the “-Confirm:$false” parameter to suppress this:


To upgrade the hardware version of the VMs, we need to make sure that the Virtual Machine is powered off and then we will upgrade this to the latest version using the Set-VM cmdlet:



The snapshot feature is most useful when you want to preserve the state of the virtual machine so you can return to the same state repeatedly. In order to create a snapshot using a native cmdlet of PowerCLI, perform the following steps:



Thanks to this amazing module, you can easily manage your vCenter infrastructure. For example, you can:

  • Create your Virtual Machine,
  • Configure your ESXi hosts,
  • Manage your VMware Cluster,
  • Automate the VM provisioning,
  • Reboot your Virtual Machines,
  • Remove Virtual Machines based on an expiration date,

Just be careful, automating tasks is very simple, but you can perform serious damages in your vCenter infrastructure. Before running your script in production, run your script in a test environment.

Follow our Twitter and Facebook feeds for new releases, updates, insightful posts and more.

Like what you read? Rate us
Managing vSphere using VMware PowerCLI
Rate this post