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:
- ArgumentException
- ArgumentNullException
- DirectoryNotFoundException
- IOException
- NotSupportedException
- PathTooLongException
- SecurityException
- UnauthorizedAccessException
However, in PowerShell I would simply catch all exceptions indiscriminately (unless you want to handle specific scenarios):
1 2 3 4 5 6 7 |
try { Copy-Item ... } catch { $exception = $_ ... } |