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.
Task
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.
that worked beautifully. thanks! strange that this is required in an mssql powershell host
LikeLike
Thanks, that saved my day!
LikeLike
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
LikeLike
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)
LikeLike
Excellent! Fixed my issue
LikeLike
Pingback: PowerShell: Copy-Item to Many Remote Computers | SQL Sanctum
Thanks very much! Saved me a lot of time fixing exactly the same problem.
LikeLike
Wow. I’ve been banging my head against the wall for about 90 minutes.
LikeLike
I had the same issue when I discovered this, glad it helped.
LikeLike
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.
LikeLike
Glad it helped! I know it caused me a lot of pain till I found such a relatively simple work around.
LikeLike
Many Thanks.
LikeLike
You made my life a bit happier, thank you for your post. Best Wishes – Covi
LikeLike
This CERTAINLY helped me out of a bad jam and a couple hours of WHAT’S WRONG WITH MY CODE ! Thanks
LikeLike
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”
LikeLike
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.
LikeLike
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.
I had two issues:
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!
LikeLike
Very Good Article….
please share for Remove-Item as well
LikeLike
Thanks! This was a definite problem-solver and time-saver.
LikeLike
Thanks!
Script #3 solved my problem.
LikeLike
Thanks!
LikeLike
Maaan I love you, it worked!
LikeLike
Thanks a lot!
LikeLike
Awesome, Thanks a lot
LikeLike
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.
LikeLike
I get this error when I try to fully qualify it The module ‘Microsoft.PowerShell.Core’ could not be loaded.
LikeLike
If you run “Get-PSSnapin” it should return that you have Microsoft.PowerShell.Core. It may be a version issue with your PowerShell (not sure how new or old yours is) or try running elevated PowerShell if possible. You get the remote copy error from locked down systems, so your system may be heavily locking down PowerShell too.
LikeLike