I have a script with several files I'd like to copy and I do it more or less like so.
Copy-Item xxx1 yyy1 -Force
Copy-Item xxx2 yyy2 -Force
Copy-Item xxx3 yyy3 -Force
Copy-Item xxx4 yyy4 -Force
and so on.
Now I'd like this script to exit with 1 if any of the files was not copied.
Thanks in advance
What you're asking for is similar to the
set -e
option inbash
, which causes a script to exit instantly in the event that a command signals failure (except in conditionals)[1].PowerShell has no such option[2], but you can emulate it:
Note:
PowerShell-internally, exit codes are not used in error handling; they typically only come into play when invoking external programs from PowerShell, or when PowerShell / a PowerShell script needs to signal success vs. failure for the outside world (when called from another shell, such as
cmd
on Windows, orbash
on Unix-like platforms).PowerShell's automatic
$LASTEXITCODE
variable reflects the exit code of the most recently executed external program / PowerShell script that calledexit <n>
.Calls to external (console/terminal) programs that signal failure via a nonzero exit code do not trigger the
trap
block, hence the explicitthrow
statement in the snippet above.[1] Note that this option has its critics, because the exact rules around when a failure is tolerated and when it causes a script to abort are hard to remember - see http://mywiki.wooledge.org/BashFAQ/105
[2] Potentially adding support for it is being discussed in this RFC proposal.