Modifying a VM with PowerCLI

Posted on November 27, 2013 by Jeff Hicks in VMware with 0 Comments

If you are responsible for managing a VMware infrastructure then PowerShell and PowerCLI should be a part of your toolkit. I’ve written a number of articles on getting started with PowerCLI which I’m going to assume you’ve already read (if not, see the links in the next paragraph). In this article I want to demonstrate how to use PowerCLI to modify an existing virtual machine. I’m going to assume you have already loaded the snapin and connected to your VMware server, so let’s begin!

(Need to catch up? Check out the rest of this series! In part one: downloading and installing VMware vSphere PowerCLI, In part two: setting up and configuring PowerCLI. Part three: use PowerCLI to start and shutdown VMs. In part four: PowerCLI and PSDrives, and in part five: created a new VM. Finally, in part six: use PowerCLI to manage your ISO files.)

Retrieving the Virtual Machine

The cmdlet we’ll be using is Set-VM. I think you’ll find it easiest to use a Get-VM expression to retrieve the virtual machine or machines and then modify them. First, make sure you can get the virtual machine you want.

Let’s modify this.


Modifying Memory

After some testing I’ve realized that this web server needs more memory assigned. Using Set-VM, it is very easy to make this change.

The cmdlet supports –WhatIf, so you can verify the change without committing to it. By the way, if you use tab completion with Set-VM you’ll notice there are two memory parameters. You can either use –MemoryGB as I’m doing here, or –MemoryMB. PowerCLI will take whatever value you specify in the corresponding units, so be careful. The image below shows a successful change.

Use PowerCLI to Modify a Virtual Machine: memory

You probably noticed that this virtual machine was turned off. If you try this with a running virtual machine, you’ll receive an error. I’d suggest that any change that is hardware related must be made with the virtual machine powered off. But some changes, such as adding a Note, should be just find with a running virtual machine.

I didn’t want to be prompted for confirmation so I “turned off” the Confirm switch.


Modifying the Hardware Version

One task you might have to do is to make sure that all of your virtual machines are at the latest hardware version. You can run into version differences by importing virtual machines or restoring from a backup. For example, I might want to find any virtual machines still at v4, which is very old, and upgrade the virtual hardware to the latest version.

Fortunately, I only have one, but you might have hundreds. Or you could use any type of filter you want. Because this process might take some time to complete, I can opt to run it asynchronously.

This will create a task on the VMware server, similar to a PowerShell background job. Use Get-Task to check the status.

You can make multiple changes with a single command, and if ever in doubt make sure the virtual machine is powered off.

In this example I got all the virtual machines named Petri something and modified the amount memory and number of CPUs.

Use PowerCLI to Modify a Virtual Machine


I knew the machines were turned off, but you can always add a filter.


There are a few other options with Set-VM which you might want to investigate, especially if you are running a cluster. As always, be sure to take the time to read full help and examples for the cmdlet and test everything with non-production systems.


Tagged with , ,