Various ways exist to copy data between a Hyper-V host and its guest machines. In this post, I will discuss the enhanced session mode, the PowerShell Copy-VMFile cmdlet, the option to mount a virtual hard disk, and using a network share.

Using the enhanced session mode

Enhanced session mode lets a virtual machine (VM) connection tool (VMConnect.exe) connect to VMs using Remote Desktop Protocol (RDP). This allows redirecting local devices and resources to guest VMs. Generation 1 and 2 VMs both support it, and no network connectivity is required between the Hyper-V host and the guest VM. Currently Windows Server 2016, Windows Server 2012 R2, Windows 10, and Windows 8.1 support this feature.

Enable Enhanced Session Mode

Enhanced connection mode is enabled by default for a Hyper-V host running on Windows 10 and 8.1. However, Windows Server 2016 and 2012 R2 require enabling it. You can do so manually via Hyper-V Manager, and there is also PowerShell cmdlet to achieve this:

Set-VMHost -EnableEnhancedSessionMode $True

How to transfer files using enhanced session mode

After enabling enhanced session mode, connecting to the VM by right-clicking the VM and clicking Connect presents you with a dialogue box to configure connection settings:

Connection settings for the VM

Connection settings for the VM

Now click Show Options to open the advanced settings. From the Display tab, check the box Save my settings for future connections to this virtual machine.

Saving VM connection settings

Saving VM connection settings

Next, select Local Resources and then click More. (Please note that the Printers and Clipboard settings are enabled by default. The clipboard lets you simply copy and paste text or files and folders from the host to guest VMs and vice versa.)

Listing local resources

Listing local resources

Now select Local Resources from the Hyper-V host and choose the disks or other plug-and-play devices you wish to make available to the VMs, and then click OK. Here I have selected just the E: drive for demonstration purpose; Smart cards is checked by default.

Select local resources for the VM

Select local resources for the VM

Once you are done with modifying the connection settings, click Connect, and then log in to the VM.

Connection settings for the VM

Connection settings for the VM

Connecting to the VM in enhanced session mode

Connecting to the VM in enhanced session mode

Resources selected in the previous steps are now available to the VM, and you can simply copy data to or from this drive by copying and pasting it.

Drive E: host is accessible from within the VM

Drive E: host is accessible from within the VM

Using the PowerShell cmdlet Copy-VMFile

To copy files to a VM using the Copy-VMFile cmdlet, we first have to enable Guest Services under Integration Services for a VM. Luckily, there is a PowerShell one-liner for this:

Enable-VMIntegrationService -Name "Guest Service Interface" -VMName "ADDNSDHCP"

Now we are now ready to copy the file from the host (source) to the guest VM (destination) using the following command:

Copy-VMFile -Name "VMName" -SourcePath 'E:\OS\Windows 7\Win7x64 GSP1RMCNENXVOL_EN_DVD.iso' -DestinationPath "C:\OS\Windows7.iso" -FileSource Host  CreateFullPath

The CreateFullPath parameter ensures creation of the destination folder if it doesn't exist. Note that the cmdlet can only copy files and not folders. You can work around this by compressing folders to a zipped file.

Example of the Copy VMFile cmdlet

Example of the Copy VMFile cmdlet

Mount the virtual hard disk to the host machine

Update: Some readers reported that mounting the virtual disk corrupted the disk.

If you can afford to shut down the VM, copying the file to the VM by mounting the virtual hard drive is the easiest and fastest way. Use the following steps:

  1. Connect to the VM, log in, and then shut down the VM.
  2. Navigate to the location where the virtual hard disk for this VM is stored.
  3. Right-click the virtual disk file and select Mount. It will map the VHDX file as a local disk drive.
Locate and mount the VM disk

Locate and mount the VM disk

Browsing the mounted virtual disk

Browsing the mounted virtual disk

You can now copy files to the virtual hard disk. When done copying the data, right-click the mounted disk drive and click Eject to dismount the virtual hard disk.

Dismount the virtual disk

Dismount the virtual disk

Now start the VM, and you can use the copied data inside the VM.

Using network share

You can also use network shares or UNC paths to access data. Thus, network connectivity between the Hyper-V host and the guest VM is mandatory. For this to work, you'll need an external virtual switch connected to the VM, and the Hyper-V host and guest VM should be on the same IP subnet.

Creating a virtual switch is simple. Right-click the Hyper-V host and select Virtual Switch Manager.

Hyper V virtual switch manager

Hyper V virtual switch manager

Next, follow these steps. Select New virtual network switch, select the network type as External, and click Create Virtual Switch.

Creating a virtual switch

Creating a virtual switch

Give the virtual switch a descriptive name, select the network adapter on the Hyper-V host this switch should connect to, and then click OK.

Creating Virtual switch 2

Creating Virtual switch 2

After creating the external-type virtual switch, connect the VM to this switch. Now the Hyper-V host and the guest VM are like two computers connected to same switch.

Next configure the IP address on both the Hyper-V host and the guest VM. Make sure that the Hyper-V host and the guest VM have IP addresses in the same subnet. For example, if the Hyper-V Host has the assigned IP address 192.168.1.10 with the subnet mask 255.255.255.0, the guest VM should have the IP address 192.168.1.x with the subnet 255.255.255.0.

The Hyper-V host and the guest VM can now communicate. Now create a shared folder on the Hyper-V host and access it from the guest VM using its UNC path.

Subscribe to 4sysops newsletter!

Conclusion

The most popular way for copying files between a Hyper-V host and a guest is using the enhanced session mode. A lesser-known approach is the PowerShell cmdlet Copy-VMFile, mounting a virtual hard disk or using a network share to exchange data between the host and the guest VM.

avatar
4 Comments
  1. Karl A Uppiano 4 years ago

    Hyper-V creates a base virtual hard disk, and then checkpoints (diffs) are created on top of that. Mounting the virtual hard disk and writing to it seems to corrupt checkpoints.

  2. Michael Lynch 4 years ago

    I found the same thing. I mounted the VHD, copied the files, then was unable to start the VM with the error: "The application encountered an error while attempting to change the state of the VM. Checkpoint operation failed. Cannot create the checkpoint."

  3. anon 4 years ago

    That really helped

  4. Shashwat 3 years ago

    Any solution to this?

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account