Question:
Simple script:
1 2 3 4 5 |
"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:
1 2 3 4 5 6 7 8 |
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:
1 2 3 4 5 |
"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 ??
Answer:
As indicated by the help the ErrorAction parameter has no effect on terminating errors, which yours is.
1 2 3 4 |
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