Copy files to and from Hyper-V Guest

written by Tomas
21 · 03 · 12

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
Author

Tomas

I have worked in IT for more than 25 years, as a consultant, system administrator and technician. As a consultant, i have worked with system integration and development. I have also managed Windows client deployment for Swedish Armed Forces. 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

Create NetNat Switch for Hyper-V

 In the latest versions in the Windows 10 client operating system, Microsoft already includes a “Default Virtual Switch”, which allows you to use Hyper-V NAT Networking, without doing any configuration changes. If you want to create an additional VM Switch which uses...

Powershell GUI to encrypt string

I’ve often found myself with tasks eligible for automation. Automating is great with PowerShell until you need to pass credentials into a script. I have seen many administrators put passwords into the body of their script. For testing purposes, this could considered a...

Hyper-V GUI in Powershell

A small powershell script that i make to test powershell script with gui. In this gui a can see all my hyper-v guest i have. I can start/stop, connect and delete machines in this...

Create NetNat Switch for Hyper-V

 In the latest versions in the Windows 10 client operating system, Microsoft already includes a “Default Virtual Switch”, which allows you to use Hyper-V NAT Networking, without doing any configuration changes. If you want to create an additional VM Switch which uses...

Powershell GUI to encrypt string

I’ve often found myself with tasks eligible for automation. Automating is great with PowerShell until you need to pass credentials into a script. I have seen many administrators put passwords into the body of their script. For testing purposes, this could considered a...

Hyper-V GUI in Powershell

A small powershell script that i make to test powershell script with gui. In this gui a can see all my hyper-v guest i have. I can start/stop, connect and delete machines in this...

Comments

0 Comments

Submit a Comment

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

Follow Us