#PSTip Copying folders using Copy-VMFile cmdlet in Windows Server 2012 R2 (Hyper-V)

In an earlier article, I showed you how to use the Copy-VMFile cmdlet. However, if you have paid attention and really used it, you will know that it cannot be used to copy a folder completely with its contents in a recursive manner.

Copying each and every single file isn’t a great experience, right?

Let us see how we can copy a complete folder to a VM.

Get-ChildItem C:\Scripts -Recurse -File | % { Copy-VMFile "WC7-1" -SourcePath $_.FullName -DestinationPath $_.FullName -CreateFullPath -FileSource Host }

Observe the -File switch parameter in the command above. It is needed because the Copy-VMFile is capable of copying files only. So, if it encounters a folder, it simply fails. So, with the above command empty folders on the source path will not be created at the target because of the -File switch parameter.

Share on: