Recently I ran into an unexpected problem when trying to copy some files remotely via PowerShell. I could have sworn that I have done this exact copy scenario in the past successfully, but this time, the computers just would not talk. The solution was fairly simple, but not at all intuitive.
Copy a file remotely from one computer to another, both drives being local to their respective computers. Network connectivity, proper permissions, etc., exists between the computers. I wanted to do this completely remote, issuing the command from a third computer.
Script #1 (Remote Copy to Remote) – Failed
This should have been a very fast copy. I knew the cmdlet I needed and even the syntax. I wanted to copy a file from one computer to another, connecting to each from my computer.
$From = "\\Server1\C$\MyFile.txt" $To = "\\Server2\C$" Copy-Item -Path $From -Destination $To
However, that did not work. Instead I got an error indicating that my path was bad! “Invalid Path” isn’t a lot of help though. I know it’s valid!
OK, so maybe I had a typo. After quadruple-checking my paths though, I was finally led to believe that it was a double hop permissions issue causing the remote copy to fail. I decided to try connecting directly to one of the computers before attempting the copy.
Script #2 (Local Copy to Remote) – Failed
I used the same script here, the only difference is I no longer need to make a remote connection to the source file, so the
$From parameter is no longer a remote call. Other options that I thought would work here were
$From = "C:\MyFile.txt" $To = "\\Server2\C$" Copy-Item -Path $From -Destination $To
However, it still did not work.
The error was different this time, and even when recreating the error, I got confused and started wondering if I had a valid problem. Even though the error now says “Source and destination path did not resolve to the same provider”, it’s still the same problem and the resolution will be the same. My final attempt was a lot better.
Script #3 (Remote Copy to Remote, Fully Qualified) – Success!
After poking around the internet, I learned what the fully qualified name for PowerShell directories should be. Providing the fully qualified name is required for any remote call. In other words, if your location starts with
\\ then you need to add
Microsoft.PowerShell.Core\FileSystem:: as a prefix. If you reference a local drive, you shouldn’t prefix the local drive. This worked perfectly, even remoting to both computers.
$From = "Microsoft.PowerShell.Core\FileSystem::\\Server1\C$\MyFile.txt" $To = "Microsoft.PowerShell.Core\FileSystem::\\Server2\C$" Copy-Item -Path $From -Destination $To
Hopefully this will save someone an hour of frustration.