Adding -Verbose to a Copy-Item command seems to make the command ignore the value of $ErrorActionPreference. Is that a bug or something that I don't understand?
I think this script should stop before it displays "Why is this displayed...".
$ErrorActionPreference="Stop"
Get-Variable ErrorActionPreference | out-default
Get-Variable ErrorActionPreference -Scope Script | out-default
Get-Variable ErrorActionPreference -Scope Global | out-default
Copy-Item This.Is.Not.There.dat $Home -Verbose
Write-Host "Why is this displayed? The script should have stopped."
Copy-Item This.Is.Not.There.dat $Home
Write-Host "This is not displayed because the script stops."
Here is the output that I get:
Name Value
---- -----
ErrorActionPreference Stop
Name Value
---- -----
ErrorActionPreference Stop
Name Value
---- -----
ErrorActionPreference Continue
Copy-Item : Cannot find path 'C:\users\John\Documents\This.Is.Not.There.dat' because it does not exist.
At C:\users\John\Documents\VerboseDoesNotStop.ps1:6 char:1
+ Copy-Item This.Is.Not.There.dat $Home -Verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\users\John\D...s.Not.There.dat:String) [Copy-Item], ItemNotFoundExce
ption
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Why is this displayed? The script should have stopped.
Copy-Item : Cannot find path 'C:\users\John\Documents\This.Is.Not.There.dat' because it does not exist.
At C:\users\John\Documents\VerboseDoesNotStop.ps1:9 char:1
+ Copy-Item This.Is.Not.There.dat $Home
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\users\John\D...s.Not.There.dat:String) [Copy-Item], ItemNotFoundExce
ption
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand