Manage Learn to apply best practices and optimize your operations.

Hyper-V PowerShell commands to delete and re-create VMs

Use PowerShell cmdlets to remove and re-create a Hyper-V VM in development. This includes removing VHDs, reconfiguring the VM and installing the OS in the new VM.

Manually removing and re-creating a VM is an extensive and tedious process, but admins can save time by executing...

just a few Hyper-V PowerShell commands.

In production, you typically have VMs deployed for developers to test line-of-business and in-house designed applications. If you've installed VMs in development, the developers might ask you to re-create them for testing purposes.

Although you can use the Remove-VM and New-VM commands to delete and re-create a VM, it's important to note that you'll be required to install the OS on the VM before developers can install applications, as they might not be familiar with the OS installation process.

Step 1: Remove a development VM

The first step is to identify the development VM and then remove it. The Hyper-V PowerShell commands below can be used to remove the virtual hard drives (VHDs) associated with the VM and then the VM itself:

Remove a development VM

As you can see above, the first line is a PowerShell variable named $ThisVM, which contains the VM name. The second command stops the VM before the removal process can be initiated. The third command gets the VM object and starts deleting the VHDs associated with it. Once all of the VHDs are deleted, the final command Remove-VM –VMName $ThisVM -force removes the VM from Hyper-V.

You might have noticed that just using Remove-VM doesn't remove the VHDs associated with the VM. You need to remove VHDs by using the Get-VMHardDiskDrive cmdlet.

Step 2: Re-create the VM

Once the VM has been removed by using the Hyper-V PowerShell commands provided in step one, you can create a new VM with the same name using the script below:

Re-create the VM

When creating a new VM for developers, you'll be required to set all the required parameters, such as configuring the VM with memory and connecting the VM to a Hyper-V switch. The PowerShell script above configures memory and connects the VM to the Hyper-V Development Switch.

Step 3: Configure VM memory (optional)

One of the most important configurations for a development VM is the dynamic memory feature. The dynamic memory feature allows you to free up memory when the development VM isn't in use and make memory available to other VMs on the same Hyper-V host. To configure development VMs to use dynamic memory, use the Set-VMMemory PowerShell cmdlet below:

Configure VM memory (optional)

Step 4: Automate OS installation

The last step is to ensure that the OS is installed in the VM. You'll need the ISO file of the Windows OS that you wish to install in the VM and the file that contains answers to the questions that are generally asked when installing a Windows OS. The Hyper-V PowerShell commands below assume that the Windows10.ISO and Windows10.VFD -- the answer file -- are stored in the C:\ISOFiles folder on the host. You might want to change the location of these files.

Automate OS installation

The last command in the PowerShell script above starts the VM. When the VM starts, it will boot from Windows10.ISO and use the Windows10.VFD file as the answer file for installing the OS.

If you combine all the Hyper-V PowerShell commands provided in steps one through four in a single script, you'll accomplish the following tasks:

  • A VM by the name of SQLTestVM will be removed, along with the VHDs associated with it. You might want to change the VM name in the $ThisVM variable in steps one and two.
  • The same VM is created on local Hyper-V.
  • The VM is configured to use dynamic memory. Step three is optional, but I recommend it for development VMs.
  • After the VM is created, the VM is started and the OS installation begins. 

Next Steps

This was last published in December 2017

Dig Deeper on Microsoft Hyper-V management

PRO+

Content

Find more PRO+ content and other member only offers, here.

Join the conversation

1 comment

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

What other PowerShell cmdlets are helpful to Hyper-V admins?
Cancel

-ADS BY GOOGLE

SearchVMware

SearchWindowsServer

SearchCloudComputing

SearchVirtualDesktop

SearchDataCenter

Close