Modifying a VM with PowerCLI

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.

​ PS C:\> get-vm "globomantics web"
Name                 PowerState Num CPUs MemoryGB
----                 ---------- -------- --------
Globomantics Web     PoweredOff 1        2.000

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.

PS C:\> get-vm "globomantics web" | Set-VM -MemoryGB 2.5 -whatif
What if: Proceed to configure the following parameters of the virtual machine with name 'Globomantics Web'?
New MemoryMB: 2560MB

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.

PS C:\> get-vm mycompanydc | set-vm -Notes "last updated $(Get-Date)" -Confirm:$False

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.

PS C:\> Get-VM | where { $_.version -eq 'v4'} | set-vm -Version v9 -whatif
What if: Proceed to configure the following parameters of the virtual machine with name 'Win7 Baseline'?

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.

PS C:\> Get-VM | where { $_.version -eq 'v4'} | set-vm -Version v9  -confirm:$false -RunAsync

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.

PS C:\> get-vm petri* | set-vm -MemoryGB 1 -NumCpu 2 -Notes "H/W modified $(Get-Date)" -confirm:$false

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.

PS C:\> get-vm petri* | where {$_.Powerstate -match "off"} | set-vm -MemoryGB 1 -NumCpu 2 -Notes "H/W modified $(Get-Date)" -confirm:$false

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.