$ErrorActionPreference = 'Stop' # make errors terminating, so try/catch sees them
try {
Copy-Item .\a.txt \\server\share\ -ErrorAction Stop
Write-Host 'copied'
}
catch {
Write-Warning "Copy failed: $($_.Exception.Message)"
exit 1
}
finally { Write-Host 'done' }
Start-Transcript -Path "$env:TEMP\run.log" -Append # record everything the session prints
Stop-Transcript
Many cmdlets raise non-terminating errors that catch ignores unless -ErrorAction Stop is set. Native programs (robocopy, python) do not throw: check $LASTEXITCODE after them. Note that robocopy uses exit codes below 8 for success.