Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

-ErrorAction Stop not work in Copy-Item

Tags:

powershell

Simple script:

"test" | Out-File "C:\existing_file.txt"
$ErrorActionPreference = "Continue"
Copy-Item "C:\existing_file.txt" "C:\NonExistingDir\file.txt" -ErrorAction Stop
"hello" | Out-Host

I have this output:

Copy-Item : Could not find a part of the path "C:\NonExistingDir\file.txt".
C:\Users\ESavin\AppData\Local\Temp\d3d410e0-79b3-4736-b7e7-5aba1ab11a12.ps1:1 знак:10
+ Copy-Item <<<<  "C:\existing_file.txt" "C:\NonExistingDir\file.txt" -ErrorAction Stop
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], DirectoryNotFoundException
    + FullyQualifiedErrorId : System.IO.DirectoryNotFoundException,Microsoft.PowerShell.Commands.CopyItemCommand

hello

Why I get "hello" in output ?? -ErrorAction Stop not work??

update:

this code:

"test" | Out-File "C:\existing_file.txt"
$ErrorActionPreference = "Stop"
Copy-Item "C:\existing_file.txt" "C:\NonExistingDir\file.txt"
"hello" | Out-Host

worked as expected. there isn't "hello" in output.

Copy-Item ignore -ErrorAction and use only $ErrorActionPreference ??

like image 987
Jack128 Avatar asked Feb 20 '12 06:02

Jack128


1 Answers

As indicated by the help the ErrorAction parameter has no effect on terminating errors, which yours is.

    The ErrorAction parameter has no effect on terminating errors (such as
    missing data, parameters that are not valid, or insufficient 
    permissions) that prevent a command from completing successfully.

Source: Get-Help about_commonparameters and http://technet.microsoft.com/en-us/library/dd315352.aspx

like image 53
Tom Avatar answered Oct 08 '22 19:10

Tom