Take this script:
Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' try { echo "ErrorActionPreference = $ErrorActionPreference" Copy-Item "this_is_a_bad_path" Write-Host "Did not catch error" } catch { Write-Host "Caught Error" }
This works as expected with this output:
ErrorActionPreference = Stop Caught Error
But if I add -verbose to the string, giving me Copy-Item -verbose "this_is_a_bad_path" , $ErrorActionPrefrence no longer used, and I get this output:
ErrorActionPreference = Stop Copy-Item : Cannot find path 'C:\Users\porter.bassett\this_is_a_bad_path' because it does not exist. At C:\Users\porter.bassett\dot.ps1:7 char:3 + Copy-Item -verbose "this_is_a_bad_path" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\Users\porter...s_is_a_bad_path:String) [Copy-Item], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand Did not catch error
To make it work correctly with -verbose turn, on, I need to add -ErrorAction Stop to the line giving me Copy-Item -verbose "this_is_a_bad_path" -ErrorAction Stop
Why can't I rely on $ErrorActionPreference when using -verbose ?
powershell
Portman
source share