You dont have javascript enabled! Please enable it!

How To Copy Files Between the Guest and the Host in #HyperV With PowerShell Direct?

2 Min. Read



In Windows Server 2012 R2, Hyper-V has a nifty feature of the Integration Components / Integration Services that allows you to inject/copy files into a running VM. The PowerShell cmdlet to do so is Copy-VMFile.

Before you start using this cmdlet, you must have Guest Services enabled on your VM (this is not on by default).

To enable Guest Services, you can run the following cmdlet:

Enable-VMIntegrationService -Name 'Guest Service Interface' –VMName <VMName>

After you enable the guest service, you can start pushing files into a VM from the Hyper-V Server by using the –FileSource Host parameter.

Copy-VMFile -Name DEMO01 -SourcePath .\ –DestinationPath 'C:\VMCopyFiles\' -FileSource Host –CreateFullPath


If you noticed the –FileSource parameter has only Host as an option. By adding the –CreateFullPath parameter, it creates the folder path you defined if it is not already present, and lastly if you use the -Force parameter, you can overwrite an existing file.

However, Copy-VMFile allows you to push files into the Guest VM only and not vice versa.

The workaround to get files out of a running virtual machine is fully documented by Mr. Hyper-V here.
You need to create a checkpoint (aka snapshot) and mount the VHD(X). However, this option is a bit tricky, because you have to wait a little bit for the flushing of the last writes to the VHD(X) file before starting the Checkpoint process – when you just wrote the file to obtain within the VM.

Introducing PowerShell Direct Features

In Windows 10 and Windows Server 2016 Technical Preview 2, the Hyper-V team introduced a new feature called PowerShell Direct.

In short, PowerShell Direct allows you to remotely manage a Windows 10 or Windows Server 2016 Technical Preview virtual machine from a Windows 10 or Windows Server 2016 Technical Preview Hyper-V host without any network connectivity.

For more information, please refer to the following post.

The big news is that starting with Windows 10 and Windows Server 2016 Build #14280 or later… The Hyper-V team went one step further and added new functionality to PowerShell Direct.

You can now move data between the virtual machine and the Hyper-V host with persistent PowerShell Direct sessions!!!

In order to do so, we need to use two cmdlets (New-PSSession and Copy-Item).

The New-PSSession cmdlet has a new parameter now: –VMName

We have now three cmdlets that can be used with PowerShell Direct:

New-PSSession –VMName <VMName> / –VMGUID <VMGUID> / –VMId <VMId>
Enter-PSSession –VMName <VMName> / –VMGUID <VMGUID> / –VMId <VMId>
Invoke-Command –VMName <VMName> / –VMGUID <VMGUID> / –VMId <VMId>

Let’s see now in action how to move files IN and OUT from a Virtual Machine.

$S = New-PSSession -VMName $VM -Credential "~\Administrator"
Enter-PSSession -Session $S
[POSH-DIRECT]: PS C:\Users\Administrator\Documents> Get-ChildItem D:\Guest\
[POSH-DIRECT]: PS C:\Users\Administrator\Documents> Exit
Copy-Item -ToSession $S -Path C:\Host\ -Destination D:\Guest\
Enter-PSSession -Session $S
[POSH-DIRECT]: PS C:\Users\Administrator\Documents> Get-ChildItem D:\Guest\
    Directory: D:\Guest

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        4/21/2016  12:27 PM             22

[POSH-DIRECT]: PS C:\Users\Administrator\Documents> Exit
Remove-Item C:\Host\ -Confirm:$false
Get-ChildItem C:\Host\
Copy-Item -FromSession $S -Path D:\Guest\ -Destination C:\Host
Get-ChildItem C:\Host\

    Directory: C:\Host

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        4/21/2016  12:27 PM             22


Thanks to the Hyper-V team for delivering value-added services and capabilities on an ongoing basis!

Make sure to check my recent Windows Server Hyper-V Cookbook for in-depth details about Hyper-V!


Photo of author
About the Author
Charbel Nemnom
Charbel Nemnom is a Senior Cloud Architect, Swiss Certified ICT Security Expert, Certified Cloud Security Professional (CCSP), Certified Information Security Manager (CISM), Microsoft Most Valuable Professional (MVP), and Microsoft Certified Trainer (MCT). He has over 20 years of broad IT experience serving on and guiding technical teams to optimize the performance of mission-critical enterprise systems with extensive practical knowledge of complex systems build, network design, business continuity, and cloud security.

Related Posts


HyperV – CSV Volumes Pause States, Event ID 5120

Windows Server 2016 and System Center 2016 Technical Preview 5 Now Available! #WS2016 #SysCtr #HyperV


Let me know what you think, or ask a question...

error: Alert: The content of this website is copyrighted from being plagiarized! You can copy from the \'Code Blocks\' in \'Black\' by selecting the Code. Thank You!