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

| , ,

Published on | Updated on March 26, 2020

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 –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 -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 VHD(X) file before starting the Checkpoint process – when you just wrote the file to obtain within the VM.

Introducing New 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 a new functionality into 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!



HyperV – CSV Volumes Pause States, Event ID 5120

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


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to Charbel Nemnom’s Blog

Get the latest posts delivered right to your inbox

The content of this website is copyrighted... please contact the author first. Thank you for visiting!