Rawpixel - Fotolia

Tip

PowerShell commands for Hyper-V update bulk VM configuration

Admins who don't have access to SCVMM can make configuration changes to multiple Hyper-V VMs at once with a few PowerShell scripts and the ForEach loop approach.

Although admins can use Hyper-V manager to modify VM configurations one by one, that takes a considerable amount of time. Instead, use PowerShell commands for Hyper-V to process bulk VM configuration updates based on your requirements.

You can configure VMs to use dynamic memory, connect VMs to a different virtual network switch and update the VM snapshot file location. In addition to PowerShell, the ForEach approach can help you modify the configuration of multiple VMs.

System Center Virtual Machine Manager (SCVMM) can also be used to update VM configuration on remote Hyper-V hosts through a central UI, but admins who only manage a few VMs might not have access to it. For example, if you're managing a few VMs for finance and sales departments, you would need to contact the SCVMM IT team to modify the VM configuration.

Update the configuration for multiple VMs

It's important to note that while some PowerShell commands for Hyper-V support specifying multiple VM names, other commands can only support one VM name. For example, when updating memory, you can only specify one VM name, as shown below:

As you can see in the above command, memory is configured only for the VM named SQLVM. If you need to update memory for several VMs, use the ForEach loop method. This method enables you to process each VM specified in a text/CSV file and then execute the command to modify memory. All you need to do is create a text file and add the VM names in it.

For example, let's say you want to update memory for VMs VM1, VM2, VM3, VM4 and VM5; you can do so by executing the PowerShell commands for Hyper-V below:

As you can see above, these PowerShell commands for Hyper-V collect VM names from $VMFile, use the ForEach loop to process each VM retrieved from the $VMFile, and then update the VM configuration for memory. This script also creates a report file by the name of MemoryReport.CSV, which is stored in the C:\Temp folder on the computer that executes the script. You can use this approach to process other configuration updates for multiple VMs, as well.

Connect to a different virtual network switch

The next example shows how to connect several VMs to a different virtual network switch, but without using the ForEach loop approach:

Change the snapshot file location

Now I'll show you how to change the snapshot file location for multiple VMs. Let's say you want to change the snapshot file location for VM1, VM2, VM3, VM4 and VM5; you can do so using the PowerShell script below:

The PowerShell commands for Hyper-V above change the snapshot file location of each VM. They also create the snapshot directory for each VM. The report provided by the script contains the VM name and whether the VM configuration change was done successfully.

Change automatic stop actions

The final example shows how you can change automatic stop actions for multiple VMs. For example, say you need to ensure VM1, VM2, VM3, VM4 and VM5 shut down automatically when the Hyper-V host shuts down. Use the Set-VM PowerShell cmdlet to modify automatic stop action, as shown below:

In the PowerShell commands for Hyper-V above, you aren't reporting if the automatic stop action was successful or not, but you can provide reporting capabilities to a script if you'd like.

Dig Deeper on IT systems management and monitoring

Software Quality
App Architecture
Cloud Computing
SearchAWS
TheServerSide.com
Data Center
Close