# PowerShell Remote Copy Workaround

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 Invoke-Command or EnterPsSession. $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.

## 25 thoughts on “PowerShell Remote Copy Workaround”

1. I am having an issue trying to copy with your solution.

$source = ‘Microsoft.PowerShell.Core\FileSystem::\\serversource\d$\path\to\folder’
$destination = ‘Microsoft.PowerShell.Core\FileSystem::\\serverdestination\C$\path\to\folder’
Copy-Item $source -Destination$destination -Recurse

Response:
‘Access is denied’

[ERROR] + CategoryInfo : NotSpecified: (:) [Copy-Item], UnauthorizedAcces
[ERROR] sException
[ERROR] + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.Pow
[ERROR] erShell.Commands.CopyItemCommand

Like

• It looks like you do not have the necessary permissions to get to either the source or destination folder. Verify that you can navigate to both folders in Windows Explorer using your credentials. You may also need to try running PowerShell As Administrator. (Right-click, Run as administrator)

Like

• I had the same issue when I discovered this, glad it helped.

Like

2. This saved my day. Originally I had a script that copied a file from my local system and was piping it to and then copying out to the remote server. suddenly it broke 2 days ago and received the error. Your fix is more straight forward and truer to copy from one location to another, it took only 5 minutes to implement and I am now back up and running thanks. This really helped and I see the compounded ROI every time I run the script.

Like

• Glad it helped! I know it caused me a lot of pain till I found such a relatively simple work around.

Like

3. Many Thanks.

Like

4. This CERTAINLY helped me out of a bad jam and a couple hours of WHAT’S WRONG WITH MY CODE ! Thanks

Like

5. thank you for resolving. I used days to clear the database backup using powershell but it does not clear the old database throwing error i use the network shared device . it throws error and does not delete the files older than 5 days please help

$daysToStoreBackups = 5 Get-ChildItem “Microsoft.PowerShell.Core\FileSystem::\\192.168.1.100\Auto Backup Test\*” |? {$_.lastwritetime -le (Get-Date).AddDays(-$daysToStoreBackups)} |% {Remove-Item$_ -Force }
“removed all previous backups older than \$daysToStoreBackups days”

Like

• Without knowing what the error is, the best I can guess is that you need to verify that you can navigate to that folder at all and that you have permissions in the folder set up properly so that whatever is executing the code can actually reach it. The removal code is valid.

Like

6. Without your post I wouldn’t be able to figure out the real issue for my problem. I was calling .ps1 file from SQL server agent job.
1. Service account for the job did not have access on the shared path
2. I wasn’t using fully qualified name for powershell.

Thanks a lot for this post!

Like

7. Awesome, Thanks a lot

Like

8. Thank you so much!
I got a hard time trying to automate a backup procedure from a local host to remote host. Your workaround worked flawlessly.

Like