How To Copy a Hyper-V Virtual Machine to a New Location

Here is a step-by-step guide for copying a stateless virtual machine hosted on a server to a RAM disk and run it from there.

Brien Posey

March 8, 2024

5 Min Read
square buttons on blue backgrounds
Alamy

In a recent article, I discussed improving Hyper-V virtual machine performance by hosting it on a RAM disk. While this can boost speed, there is a major downside: RAM disks are volatile, meaning they instantly purge all their contents when the machine powers off or reboots.

Since RAM disks are a volatile storage type, you should use them exclusively for stateless workloads. You should also keep a copy of the VM on a normal storage volume. That way, you don’t have to recreate the VM from scratch each time. Alternatively, you could automate VM setup with a script that handles the unattended OS installation.

For this article, let’s assume we have a stateless VM hosted on a server and mount a RAM disk during each boot. Our goal is to copy the VM to the RAM disk and run it from there.

While we can easily script this process, I will walk you through it step by step.

Exporting the Virtual Machine

The first step is to export the virtual machine. The export action creates a copy of the VM that we can use to generate additional instances as needed.

To export a VM, open an elevated PowerShell session and enter the Export-VM cmdlet. Specify the VM name and the target folder (the location for storing the exported copy) as parameters. For example, to export a VM named VM to the D:\VMs folder, use this command:

Related:Comparing Windows VM Installation Times: HDD vs. NVMe vs. RAM

Export-VM -Name VM -Path D:\VMs

PowerShell interface notifies the user that the virtual machine is being exported

Figure 1. I am exporting the virtual machine.

Important notes:

  1. You can export a virtual machine to any path. Nevertheless, note the exact export path. You will need this information later.

  2. The export process only creates a reproducible copy of the VM. It does not affect the original VM, which stays in its original location (see Figure 2).

Screenshot shows the virtual machine in its original location and unchanged by the export process

Figure 2. The original virtual machine remains unaffected.

Locating the exported VM identifier

After the export, the target folder contains a complete copy of the virtual machine. You must identify the VM's identifier assigned by Hyper-V to create clones. Follow these steps:

  1. Open File Explorer and navigate to the folder specified during export (e.g., D:\VMs).

  2. Open the subfolder named after the VM (e.g., D:\VMs\VM\Virtual Machines).

  3. Locate the file with a .VMCX extension and note its name. The file name represents the VM's identifier (e.g., BD45FCC2-F93A-492D-A9F5-0CB58B73686D.vmcx).

Next steps

Where you go from here depends on your goal. Choose one of the following approaches:

  • Replace the Original VM: Delete the original VM and use the exported copy to spawn a new VM each time the RAM disk is mounted.

  • Run Concurrently: Keep the original VM intact and run the RAM disk VM as a separate instance.

Related:Executing a Zero-Downtime Storage Hardware Refresh

Replacing the VM

If you want to replace the VM with a copy hosted on a RAM disk, follow these steps:

1. Backup the Original VM: Always back up the VM before proceeding to ensure you can recover it if needed.

2. Import the VM to the RAM Disk: Use the Import-VM cmdlet to load the exported VM into the desired location. For example, if the exported VM resides in D:\VMs and is named VM, the command might look like this:

Import-VM -Path 'D:\VMs\VM\Virtual Machines\BD45FCC2-F93A-492D-A9F5-0CB58B73686D.vmcx' -copy -VHDDestinationPath 'C:\VMs\VM' -VirtualMachinePath 'C:\VMs\VM'

In this example, the VM is imported to C:\VMs. However, in practice, you would specify a path on your RAM disk. In Figure 3, note that the command refers to the .VMCX file mentioned earlier.

Showing that the .VMCX file is needed

Figure 3. The import command must reference the exported VM’s .VMCX file.

PowerShell screenshot displays the import process

Figure 4. This is what the import process looks like.

3. Review the Import Process: The import process creates a fully functional Hyper-V virtual machine. The new VM will reside at the location specified by the import command (Figure 5).

4. Preserve the Exported Process: The original exported VM remains intact, as shown in Figure 6. This allows you to reuse the exported copy to spawn additional VMs as needed.

Showing that the new VM resides at the location specified by the import command

Figure 5. The import command has created a new virtual machine.

Related:Tracking Down Lost Disk Space: A PowerShell Solution

Showing that the exported copy still exists


Figure 6. The exported copy still exists.

Creating a Duplicate VM

In some cases, instead of replacing the original VM, you may want to create a duplicate hosted alongside it. While simultaneously running both copies may not always be ideal depending on their configurations, duplicating the VM can be valuable for testing or redundancy.

The command for creating a secondary VM copy resembles the one I shared earlier:

Import-VM –Path 'D:\VMs\VM\Virtual Machines\BD45FCC2-F93A-492D-A9F5-0CB58B73686D.vmcx' -Copy -GenerateNewID

Note this command uses the -Copy and the -GenerateNewID parameters. The -GenerateNewID parameter assigns a unique identifier to the new copy, allowing it to coexist with the original. You can see the command in action in Figure 7.

PowerShell screenshot shows Import-VM cmdlet being used to create a secondary virtual machine copy

Figure 7. You can use the Import-VM cmdlet to create a secondary VM copy.

When the command finishes, Hyper-V will host two identical VMs capable of running side by side (Figure 8).

Showing that Hyper-V has two identical virtual machines running simultaneously

Figure 8. There are now two VM copies.

The two copies, while identical, are stored in separate paths (Figure 9).

Showing that the two virtual machine copies are stored in separate paths

Figure 9. The two VM copies exist separately, with files stored in differing locations.

About the Author

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

https://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like