How to Copy Files to Hyper-V Server and VMs: 7 Methods

Hyper-V is a hypervisor, which runs on Windows, and can be managed directly on the machine where it is installed as well as remotely. Often, you need to copy files to Hyper-V Server and from a server to virtual machines. You cannot just drag and drop files to Hyper-V VMs, and you should use other methods to copy files.

For example, when creating a new virtual machine in Hyper-V, you need to install an OS on that VM. Generally, this is done by mounting an ISO installation image of the operating system media. To get the media to the Hyper-V host, you have to copy the ISO file to the host and install the guest operating system on the new VM.

This blog post provides 6 methods for how to copy files to Hyper-V virtual machines and to Hyper-V Server. Note that these methods to copy files to Hyper-V server can also be used in other scenarios, such as copying files from a Hyper-V host to VMs.

NAKIVO for Hyper-V Backup

NAKIVO for Hyper-V Backup

Agentless, application-aware backups for Hyper-V. Anti-ransomware protection, instant VM boot from backup, other virtual/physical platform support and more.

Method 1: How to Copy Files Using a File Share on a Hyper-V Host

This method involves creating a file share on a Hyper-V host and copying an ISO image or other files to this SMB file share. The SMB protocol is a native sharing protocol in Windows but is also supported in Linux and other operating systems.

You can also use this method to copy files to Hyper-V server from any physical or virtual machine if a network connection is established.

  1. Enable a firewall rule to enable inbound SMB connections for a machine running Hyper-V. To enable file and printer sharing in the firewall, run the following command on the Hyper-V host:

netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=Yes

Note: You can configure an SMB file share with the appropriate permissions for the needed users and share only the needed folder.

  1. If you have the graphical user interface (GUI) on your Windows machine, connect to a remote file share on a Hyper-V host by typing the share path in the address bar of the Windows Explorer window. In this example, we use a hidden system share C$, which is enabled by default on all Windows machines for administration purposes. Similarly, you can connect to other volumes like D$, E$, etc.

\\10.10.10.31\c$\

Note: Files are stored on disk C: for demo purposes. It is recommended that you use another dedicated partition for VM files, ISO images, file shares, and other non-system data.

  1. Browse to the directory where you want to store your ISO images or where the other files that you need to copy are located. You can create this directory now if it has not been created yet. In our case, the ISO files are located in C:\ISO_1\ on our Hyper-V host and the network path is \\10.10.10.31\c$\ISO_1\

If you need to use the command line interface, you can map the SMB file share as a network drive with the command:

Net use * \\〈your hyper-v host〉\d$

For example,

net use S: \\10.10.10.31\d$

Note: You can run this command in CMD or PowerShell on the client or server that has the data that you want to transfer to Hyper-V.

  1. Copy the needed ISO file (or other files) from your local machine (C:\data\shared01\ in our case) to the Hyper-V host by using this network path.

How to copy files from Hyper-V to host

  1. After copying the ISO image to the server, we can point the DVD drive to the image file and browse to the folder that we created on the root of the (C:) drive.

How to add ISO to Hyper-V

  1. After you finish copying all needed files to your Hyper-V host, you can disable inbound connections in the Windows firewall on the Hyper-V host for security purposes. We can disable the file and printer sharing firewall rules we allowed above if we choose to do that by simply changing Yes to No in the PowerShell command.

netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=No

Method 2: How to Copy Files to Hyper-V Servers Using a Single File Share

When you have multiple Hyper-V hosts in your environment, it can be inconvenient to configure a file share on each Hyper-V host and copy ISO images to all these Hyper-V hosts. Moreover, using storage space on multiple hosts for the same ISO images may be not rational.

You can configure a file share on another server, for example, on a file server or NAS and store all the needed ISO images there. Configure a file share once, configure access for Hyper-V hosts, and use the needed ISO images stored in one place for all hosts.

Hyper-V share folder to access ISO files

This method is convenient if all used machines are members of an Active Directory domain.

If you want to use this method to transfer files between a Hyper-V host and a virtual machine’s guest operating system, you need to configure a virtual switch in Hyper-V settings and set the needed network mode.

If you need to copy files to a virtual machine with a Linux guest OS, you have another option in addition to using the SMB protocol. You can use SSH for secure Hyper V file transfer over the network.

Method 3: Hyper-V File Transfer via RDP

If your Hyper-V Server is running on a Windows machine with the GUI, you can connect to this host via RDP (Remote Desktop Protocol) and attach the local disks of your local machine to copy the needed files. To do this, you need to enable remote desktop connections on the target server.

You can use this method to copy files to Hyper-V server from another physical or virtual machine running Windows.

  1. To enable remote desktop connections, go to Start > Settings > System > Remote Desktop and enable Remote Desktop.
  2. Run the Windows Remote Desktop client, whose location is %windir%\system32\mstsc.exe

Type mstsc in the command line or Run dialog and press Enter.

  1. Enter the IP address or hostname of the remote computer (in our example, it is the Hyper-V host with the IP address 10.10.10.31) and the username. You can enter the username in the full format (domain\user or computer\user).

Using RDP for Hyper V file transfer

  1. Go to the Local Resources tab and click More.

How to attach local disks and copy files to Hyper v server via RDP

  1. Select the local resources you want to map from your computer to the remote host (in our example, the remote host is a Hyper-V host). Expand Drives and select the needed disk drive. The needed files are located on disk E:\ on our computer and, for this reason, we select disk E: to be mapped for our RDP connection.
  2. Click OK to save the settings and then click Connect.

Selecting a local disk for an RDP connection to add ISO to Hyper-V server

Once a remote desktop session has been established, open My Computer or This PC in Windows Explorer of the remote machine. You should see the mapped disk drive from your local computer that is mapped to the remote machine (E on SERVER01-DC in the screenshot below). Browse to the needed files on this mapped drive and copy them to your Hyper-V host.

The local disk is connected to the remote Hyper-V host in an RDP session

This method also works for VMs if they are connected to the network.

Method 4: Hyper-V File Transfer with a PowerShell CMDlet

PowerShell 4.0 and higher has a useful cmdlet that allows you to copy files from a Hyper-V host to a virtual machine without having a network connection between the host and VM guest and without the need to configure the firewall.

So let’s look at how to copy files to a Hyper-V virtual machine in PowerShell without using the network.

  1. Enable Guest services on the virtual machine. Integration services must be installed in the guest OS of the VM.

Guest services are a part of Hyper-V Integration services, but they are not enabled by default. To enable Guest services, open VM settings, select Integration Services in the left pane of the VM settings window, and select the Guest services checkbox.
Selecting Guest services for Hyper-V file transfer between a host and guest

Enable Guest services for the VM in PowerShell:

Enable-VMIntegrationService -VMName VM1 -Name 'Guest Service Interface'

  1. You can check whether the Guest services feature is enabled in PowerShell:

Get-VMIntegrationService -name Guest* -VMName VM1,VM2

Where VM1,VM2 are the VM names

  • If the status is No contact, you need to update the VM and install Integration Services in the guest OS.
  • If the status is False, you can enable the needed guest services interface:

Enable-VMIntegrationService -name Guest* -VMName win10preview -Passthru

  1. Use the command like below for Hyper-V file transfer from the Hyper-V host to VM guest by setting the source path on the Hyper-V host and the destination path on the VM:

Copy-VMFile "VMName" -SourcePath "F:\Test.txt" -DestinationPath "C:\Temp\Test.txt" -CreateFullPath -FileSource Host

Where:

  • VMname is the name of the VM to which you’re copying the file (you can see the VM name in Hyper-V Manager; this is not the hostname of the VM displayed in the guest OS).
  • SourcePath is the path to the source file on the Hyper-V host (the file you need to copy).
  • DestinationPath is the path to the destination file on the VM (where to copy the needed file).
  • The -CreateFullPath command is used to create a full path with all needed folders if they don’t exist.

In our example, the exact PowerShell command for Hyper V file transfer from a host to VM guest is:

Copy-VMFile "VM2" -SourcePath "C:\temp\file01.txt" -DestinationPath "C:\Temp1\file01.txt" -CreateFullPath -FileSource Host

Note: the Copy-VMFile cmdlet works only in the direction from a Hyper-V host to VM.

Method 5: Mounting VHDX to a Host and Guest

The idea of this method is to create a VHDX virtual disk that can be mounted to both a Hyper-V host and VM. You can copy the needed files to this virtual disk, mount the disk to the needed machine, and copy files to/from the virtual disk.

We use Windows Server 2019 in our example. Creating a VHDX virtual disk is available in Computer Management, not only in Hyper-V Manager when configuring VMs.

  1. Open the Computer Management window. You can do it by pressing Win+R to open the Run dialog and running the compmgmt.msc command.
  2. Go to Storage > Disk Management in the Computer Management window.
  3. Right-click Disk Management and in the context menu click Create VHD.

Creating a VHD file on a Hyper-V Server

  1. A new window opens to create a virtual disk.
  • Specify the virtual hard disk location on the Hyper-V host, for example, C:\Hyper-V\data-temp\
  • Set the virtual hard disk size. We create an 8-GB virtual hard disk for demo purposes.
  • Select a virtual hard disk format. We select VHDX as the newer format. Read about VHD vs VHDX to learn more about the differences.
  • Select a virtual hard disk type. We select Dynamically expanding to save storage space until the disk is not filled with the data.
  • Hit OK to save settings and close the window.

Configuring parameters for a new VHDX file

Alternatively, here is an example of the command to create a VHDX virtual disk in PowerShell:

New-VHD -Path C:\Hyper-V\data-temp.vhdx -SizeBytes 8GB -Dynamic -BlockSizeBytes 1MB

The command to mount a VHD virtual disk in PowerShell:

mount-VHD -path C:\Hyper-V\data-temp.vhdx

A virtual hard disk has been created on our Hyper-V host.

  1. Right-click this VHDX virtual hard disk in the Disk Management snap-in and click Initialize Disk.

Alternatively, if you prefer to use PowerShell, then list all mounted disks to identify the number of the needed virtual disk before initialization:

Get-Disk

Initializing a disk for Hyper-V file transfer

  1. Your disk is automatically selected. Select the partition style. We use MBR (Master Boot Record) for better compatibility. Click OK.

Alternatively, the command for those who use PowerShell includes the number of a virtual disk and partition style:

Initialize-Disk 1 -PartitionStyle MBR

Selecting the MBR partition style

  1. Create a new volume (partition) on the virtual disk that has been initialized on the Hyper-V host. This partition will also be visible in a Windows VM (or Linux that can work with NTFS file systems).

Right-click the 8-GB volume and click New Simple Volume in the context menu.
Creating a new simple volume

  1. The New Simple Volume Wizard opens. Click Next.
  2. Specify the volume size. Use the entire disk size for this volume.

Specifying the volume size

  1. Assign a drive letter, for example, H:

Selecting a drive letter

  1. Format the partition. Select the NTFS file system, leave the default allocation unit size, and enter a volume label (for example, Copy-Files). Select the Perform a quick format checkbox.

Formatting a partition that will be used for Hyper-V file transfer

Alternatively, you can use these PowerShell commands to create and format a volume:

New-Partition -DiskNumber 1 -UseMaximumsize -AssignDriveLetter

Format-Volume -DriveLetter H -FileSystem NTFS -Full -Force

  1. Once you have completed the wizard, you should see your mounted virtual disk in Windows Explorer of the Hyper-V Host machine.
  2. Copy the needed files to the partition located on the virtual disk. We copy file01.txt, as shown in the screenshot below.

How to copy files to Hyper V virtual machine by using a virtual disk

  1. We must unmount a virtual disk from our Hyper-V host after copying all needed files to the virtual disk.

Right-click the appropriate partition mounted from the VHDX virtual disk and click Eject in the context menu.

Alternatively, the command to dismount a virtual disk from a host machine in PowerShell:

dismount-VHD -path C:\Hyper-V\data-temp.vhdx

Eject the virtual disk after you have copied the files

  1. You can mount this virtual disk created for Hyper V file transfer from a Hyper-V host to a guest and vice versa. Let’s mount a virtual disk with written files to the virtual machine.
  1. Select the needed VM and open VM Settings in Hyper-V Manager.
  2. Select the needed virtual disk controller in the left pane of the VM settings window.
  3. Select Hard Drive and click Add.

Adding a new virtual hard disk drive to the VM configuration

  1. Select Virtual hard disk and click Browse to add the existing virtual hard disk to the VM.
  2. Specify the VHDX path. In our case, the path is C:\Hyper-V\data-temp.vhdx
  3. Hit OK to save settings.

Alternatively, this is PowerShell command to add an existing virtual hard disk to a Hyper-V VM configuration:

Add-VMHardDiskDrive -VMName VM2 -Path C:\Hyper-V\data-temp.vhdx

Browsing a virtual disk you have created to transfer files from Hyper-V to host (or from host to VM)

  1. Start the virtual machine with the attached virtual disk on your Hyper-V host.
  2. Log in to a guest operating system and check whether the volume located on the virtual disk has been mounted automatically. Otherwise, go to Computer Management > Disk Management and assign a disk letter to the volume with the files you need to transfer from the Hyper-V host to the VM.
  3. Copy the needed files from the attached virtual disk to the virtual machine.

Method 6: VMConnect with Enhanced Session Mode

VMConnect, or Virtual Machine Connection, is a built-in Hyper-V tool to manage virtual machines. A screen operating as a monitor for the VM where you can see how you navigate with a keyboard and mouse is a part of the VMConnect tool. Using VMConnect with the enhanced session mode is more convenient and provides more features.

The enhanced session mode allows you to customize screen resolution when connecting to a VM and attach local resources of a host on which you run Hyper-V Manager to manage VMs. The local resources are attached similarly as when using a Remote Desktop connection to access a remote host or VM.

To enable Enhanced session mode:

  1. Right-click the name of your Hyper-V host in Hyper-V Manager and in the context menu, click Hyper-V Settings.
  2. Click Enhanced session mode in the left panel
  3. Select the Use enhanced session mode checkbox

How to enable the Enhanced session mode for Hyper-V file transfer

Alternatively, enable the Enhanced session mode in PowerShell:

Set-VMHost -EnableEnhancedSessionMode $True

Once the Enhanced session mode is enabled and everything is configured for this in the guest VM and Hyper-V server, you can start the VM and connect to this VM with VMConnect.

You should see the window for selecting display resolution if the Enhanced session mode is enabled. Click Show Options in this window.

Select Show Options to see additional options

Select the Local Resources tab. Click More in the Local devices and resources section.

Click More to attach local disks to a VM connection session

Select disks available on your Hyper-V host that you want to connect to a VM.

Hyper-V copy files to VM – selecting a disk when using the enhanced session mode for VMConnect

Method 7: Hyper-V File Transfer Using ISO Images

Another method for how to copy files to Hyper-V virtual machine is similar to using a VHDX virtual disk. However, this one is only in one direction (copy Hyper-V files to VM), and access to the files from the VM is read-only.

If you have an application that can edit and write ISO files that are CD/DVD image files, you can write the needed files on your Hyper-V host to the ISO image and insert this ISO image as a virtual DVD medium into the VM.

If you need to copy a bootable ISO image to your virtual machine (for example, when testing nested virtualization), you should create a new empty non-bootable ISO image file and add a bootable ISO image file to this image. Then you can copy the bootable ISO image from the outer ISO file inserted into the virtual DVD drive of the VM.

Conclusion

Whichever method you use to copy files to Hyper-V Server, it is not recommended to use it for backup purposes. These methods are not as efficient and reliable as professional Hyper-V backup solutions. Today’s Hyper-V VM backup solutions, like NAKIVO Backup & Replication, create host-level backups that are small, fast, and easy to recover data from.

1 Year of Free Data Protection: NAKIVO Backup & Replication

1 Year of Free Data Protection: NAKIVO Backup & Replication

Deploy in 2 minutes and protect virtual, cloud, physical and SaaS data. Backup, replication, instant recovery options.

People also read