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 do I get hi output? -ErrorAction Stop not working?
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 properly. there is no hello output.
Copy-Item ignore -ErrorAction and use only $ ErrorActionPreference ??
source share