Copy files to and from Hyper-V Guest

by Tomas | Mar 12, 2021 | Featured Post | 1 comment

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 IntegrationService for Guest

Enable-VMIntegrationService -Name "Guest ServiceInterface" -VMName "WS2019-DC01"

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

Copy File from Host To Guest VM

Copy-VMFile "WS2019-DC01" -SourcePath "D:\Test.txt" -DestinationPath "C:\Temp\Test.txt" -CreateFullPath -FileSource Host

However, you cannot use this command when you need to copy files from guest to host. You then need to create a PSSession and then run the command to copy files from guest to host.

Using PSSession for copy file from Guest to Host

<#
Solution: Microsoft Hyper-V Tool
 Purpose: Copy from Hyper-V Guest
 Version: 2.0.0
    Date: 12 March 2021

  Author: Tomas Johansson
 Twitter: @deploymentnoob
     Web: https://www.4thcorner.net

This script is provided "AS IS" with no warranties, confers no rights and 
is not supported by the author
#>

# Define a Microsoft .NET Core class in your PowerShell session
Add-Type -AssemblyName Microsoft.VisualBasic

# This command uses the PromptForCredential method to prompt the user for their user name and password.
# The command saves the resulting credentials in the $Credential variable.
$Credential = $Host.UI.PromptForCredential("Need credentials", "Please enter your User name and Password.", "", "NetBiosUserName")

# Server to use the PSSession to 
$ConnectToServer = [Microsoft.VisualBasic.Interaction]::InputBox('Input Server to connect to', 'Server Name', "")

# Create new PSSession to Server with specific Credential
$Session = New-PSSession -VMName $ConnectToServer -Credential $Credential

# Start interactive session with single remote computer
Enter-PSSession  -Session $Session

# Copy Items from Temp Diretory from remote computer and if file and overwrites existing files
Copy-Item -FromSession $Session -Path "C:\TEMP\*" -Destination "D:\Temp" -Force

# Closes the current session. also closes the connection between the local and remote computers
Remove-PSSession -Session $Session

Written by Tomas

I have a broad experience of most things in IT and still has a burning interest in learning new technologies and how to best use the technology.

Related Posts

Austria buys the SAAB Draken II
Austria buys the SAAB Draken II

Austria today presented the successor to the Eurofighter Typhoon interceptor: The Federal Army will be the launch customer for the new Saab 41 Draken II - Sweden's first 5th generation fighter.   Quantity and purchase price still under negotiation © Mobius / YThis was...

read more
I have a new job
I have a new job

From 2023-06-01 I start work as a Software Engineer (Systems) at SAAB Surveillance Systems. SAAB Surveillance Business area Surveillance provides efficient solutions for safety and security, for surveillance and decision support, and for threat detection,...

read more

1 Comment

  1. Joe OBremski

    But how do you make COPY-VMFILE work when copying a file to a VM on another HOST server in like a cluster enviroment. -COMPUTERNAME seems like the solution but I get 80070005 Access denied anytime I point to a different hyper-v host then the one I’m on.

    When it works on the local host the Hyper-V event log shows a successful entry from the user “NT VIRTUAL MACHINE\{VM sid}”.

    I’m assuming I need to allow some permissions somewhere?