Why does -Verbose make Copy-Item ignore ErrorActionPreference?

John Vottero

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
mklement0

It is a known bug in Windows PowerShell that has since been fixed in PowerShell [Core] as of (at least) v7.0.3, which is the LTS version as of this writing (I'm not sure in what specific version the fix was introduced).

Here's a minimal example to reproduce the problem:

& { 
  $ErrorActionPreference = 'Stop'
  Get-Item \no\such\path -Verbose
  '!! Should not get here' 
}

With the bug present, the string !! Should not get here unexpectedly prints, even though execution should have been aborted due to the Get-Item call triggering a non-terminating error.

A statement-terminating error, by contrast, is not affected by the bug; e.g., if you use Get-Item -NoSuchParam -Verbose instead, execution is aborted as expected.

Given that Windows PowerShell is no longer being actively developed, this bug may never get fixed - though perhaps if enough users show interest in the open bug report, it will (note that the report only mentions Move-Item, but it seems that all cmdlets are affected).


Workarounds:

  • Instead of -Verbose, use the $VerbosePreference variable.
& { 
 $ErrorActionPreference = 'Stop'
 $VerbosePreference = 'Continue' 
 Get-Item \no\such\path
 '!! Should not get here'
}
  • Instead of $ErrorActionPreference = 'Stop', use -ErrorAction Stop on every cmdlet call:
& { 
 Get-Item \no\such\path -Verbose -ErrorAction Stop
 '!! Should not get here'
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does Robocopy ignore the /copy flags?

From Dev

Why does make ignore my modifications?

From Dev

Copy-Item does not displays verbose messages even when $VerbosePreference is 'Continue'

From Dev

Why does make ignore escape sequence if the output is piped

From Dev

Define folder depth for the verbose option in Copy-Item cmdlet

From Dev

Why does binding to const reference from a ternary make a copy?

From Dev

Why does boost::make_shared use copy semantics

From Dev

Why will spawning make a copy?

From Dev

Why does my loop ignore and add NULL to every second item in the database?

From Dev

Why does Git ignore these folders?

From Dev

Why does FloatToStr ignore ThousandSeparator?

From Dev

Why does setInterval() ignore errors?

From Dev

Why does GLSL ignore return?

From Dev

Why does sudo ignore aliases?

From Dev

Why does bash ignore SIGTERM?

From Dev

Why Gulp does not ignore the file?

From Dev

Does ConcurrentHashMap make a copy of itself?

From Dev

Make a copy of a set and exclude one item

From Dev

Why does MSBuild copy task not copy?

From Dev

Why does MSBuild copy task not copy?

From Dev

Why does the Copy Constuctor fail to "copy"

From Dev

Why post-increment needs to make a copy while pre-increment does not

From Dev

Why does Entity make a duplicate of related item when trying to add readonly object?

From Dev

How does Copy-Item behave?

From Dev

Java on Android: Does adding item to ConcurrentLinkedQueue copy it

From Dev

Why does AngularJS ignore $scope field change?

From Dev

Why does this code ignore the await and proceed anyway?

From Dev

Why does Python requests ignore the verify parameter?

From Dev

Why does Git ignore the .vs directory?

Related Related

HotTag

Archive