How to handle Copy-Item exceptions?

Question:

I am writing a PowerShell script which uses Copy-Item. I would like to know what kind of exception will be thrown when Copy-Item fails to copy the files from source to destination?

From this link, I don’t see what kind of exception will be thrown.

I want to handle the exception in case of failure.

Answer:

Since PowerShell is based on .NET I would expect the exceptions that are defined for the CopyTo() and CreateSubdirectory() methods:

However, in PowerShell I would simply catch all exceptions indiscriminately (unless you want to handle specific scenarios):

Source:

How to handle Copy-Item exceptions? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply