1

I'm seeing a strange behavior with the "Copy-Item" cmdlet. It looks like the cmdlet errors out if the source location is a local location that does not exist, but completely ignores that if the source is a network location that does not exist.

For e.g.

Copy-Item \\server\share\thisdoesnotexist\* -Destination c:\temp -ErrorAction Stop

This command doesn't show any errors even if the source share is invalid. However,

Copy-Item c:\thisdoesnotexist\* -Destination c:\temp -ErrorAction Stop

throws an error that looks like:

Copy-Item : Cannot find path 'C:\thisdoesnotexist' because it does not exist.
At line:1 char:1
+ Copy-Item c:\thisdoesnotexist\* -Destination c:\temp -ErrorAction Stop;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\thisdoesnotexist:String) [Copy-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

What's going on here? I checked TechNet but couldn't see anything there that talked about this. My question is:

  • Is this expected?

  • Is there a way to make the cmdlet fail when source network share is invalid as well?

Thanks for any pointers!

4
  • I can confirm these finding with PowerShell 3.0. Might be a bug but it would be good measure to test these paths before hand with Test-Path anyway. This might be because of the 2 different providers
    – Matt
    Apr 8, 2015 at 23:36
  • Both are using the FileSystem provider, right? Apr 9, 2015 at 0:41
  • Also happens on Powershell 4.0. When you remove the asterisk it will give you the correct error. In any case, you should always test your paths before you do a Copy-Item (see Test-Path) Apr 9, 2015 at 14:40
  • Thanks folks. I'll go with Test-Path before the copy to make sure they exist. I also posted this on connect.microsoft.com/PowerShell/feedbackdetail/view/1234831/…. If any of you can vote on it, maybe it'll help get traction on their end.
    – Snooks
    Apr 13, 2015 at 20:37

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.