
close
close
Copying files to Hyper-V virtual machines wouldn’t seem like a big deal. In most situations, the virtual machine is no different than a physical machine on your network. You could copy files to a virtual machine using traditional methods like you would any other machine, but sometimes that isn’t possible. Fortunately, there is an alternative. If you are running the latest version of Hyper-V on either Windows Server 2012 R2 or Windows 8.1, which implies that you’re running PowerShell 4.0, then you have access to a new cmdlet in the Hyper-V module called Copy-VMFile.
I’m going to demonstrate how to use this cmdlet, but be sure to take time to read the help.
help Copy-VMFile -ShowWindow
Copy-VMFile help in Windows PowerShell. (Image Credit: Jeff Hicks)
Get-VMIntegrationService -name Guest* -VMName chi-dc01,chi-dc02,win10preview
Verifying that the Guest Services feature is enabled in Windows PowerShell. (Image Credit: Jeff Hicks)
Enable-VMIntegrationService -name Guest* -VMName win10preview -Passthru
Enabling Guest Services in Windows PowerShell. (Image Credit: Jeff Hicks)
$paramHash = @{
Name = 'chi-dc01','win10preview'
SourcePath = 'C:\work\file.txt'
DestinationPath = 'C:\work\file.txt'
CreateFullPath = $True
FileSource = 'Host'
Force = $True
Verbose = $True
}
My intention is to copy C:\work\file.txt to the same path on CHI-DC01 and Win10Preview virtual machines.
Copy-VMFile @paramHash
Copying files using Copy-VMFile in Windows PowerShell. (Image Credit: Jeff Hicks)
invoke-command { get-item c:\work\file.txt} -computer chi-dc01,chi-win10
Verifying that the copy file operation completed successfully. (Image Credit: Jeff Hicks)
The file was successfully copied to the correct location. (Image Credit: Jeff Hicks)
dir c:\files | foreach {
$destination = Join-path -Path "C:\files" -ChildPath $_.name
write-host "copying $($_.fullname) to $destination" -foreground Yellow
$paramHash.SourcePath = $_.fullname
$paramHash.DestinationPath = $destination
Copy-VMFile @paramHash
}
My goal is to copy all the files under C:\Files to CHI-DC01 and my Windows 10 virtual machines. I’m taking each file and updating parameters in the hashtable.
Prepping my hashtable for multiple files. (Image Credit: Jeff Hicks)
get-vm -ComputerName chi-hvr2| where {$_.state -eq 'running' -AND -Not ((Get-VMIntegrationService -Name Guest* -vm $_).Enabled)}
These virtual machines fail that test.
Verifying that Guest Services is enabled. (Image Credit: Jeff Hicks)
get-vm -ComputerName chi-hvr2|
where {$_.state -eq 'running' -AND -Not ((Get-VMIntegrationService -Name Guest* -vm $_).Enabled)} |
Enable-VMIntegrationService -Name Guest* -Passthru
It looks like there is a problem.
But usually this is temporary. I can always re-verify.
Get-VM -ComputerName chi-hvr2 | where {$_.state -eq 'running'} | Get-VMIntegrationService -Name Guest*
$paramHash = @{
SourcePath = 'c:\work\file.txt'
DestinationPath = 'c:\work\file.txt'
Force = $True
CreateFullPath = $True
FileSource = 'Host'
Verbose = $True
}
Get-VM -ComputerName chi-hvr2 |
where {$_.state -eq 'running'} |
Copy-VMFile @paramHash
Here’s the command in action:
Copying a file to a guest. (Image Credit: Jeff Hicks)
More in Hyper-V
Most popular on petri