Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell: ErrorAction set to "SilentlyContinue" not working

The following command does not show the error message, which is what I want:

Copy-Item "C:\Folder I Have Access To\*" "C:\Folder I Do Not Have Access To" -ErrorAction SilentlyContinue

The following command does show the error message, which is not what I want:

Copy-Item "C:\Folder I Have Access To\*" "C:\Folder I Do Not Have Access To" -Force -ErrorAction SilentlyContinue

This is because I'm using the "Force" parameter. Is there a way I can use the "Force" parameter and still not show the error message?

like image 361
Simon Avatar asked Jun 14 '11 15:06

Simon


People also ask

What is ErrorAction SilentlyContinue in PowerShell?

This value is rarely used. -ErrorAction:SilentlyContinue suppresses the error message and continues executing the command. -ErrorAction:Stop displays the error message and stops executing the command. -ErrorAction:Suspend is only available for workflows which aren't supported in PowerShell 6 and beyond. Note.

What is ErrorAction in PowerShell?

The PowerShell ErrorAction parameter allows handling the actions if any error occurs. By default, PowerShell operates on the continue option for error handling. However, the ErrorAction operator can be used to change the default option.

How do I stop error messages in PowerShell?

If you need to suppress an error, you can use the ErrorAction switch to suppress an error for a single cmdlet or an ErrorAction preference variable to suppress errors globally.


2 Answers

Add this first.

$ErrorActionPreference = "silentlycontinue"
like image 181
Mark Poulton Avatar answered Sep 19 '22 12:09

Mark Poulton


Can you try this :

trap
{
  continue
}
Copy-Item "C:\Folder I Have Access To\*" "C:\Folder I Do Not Have Access To" -Force -errorAction SilentlyContinue

or

try
{
  Copy-Item "C:\Folder I Have Access To\*" "C:\Folder I Do Not Have Access To" -Force -errorAction SilentlyContinue
}
catch
{
}
like image 38
JPBlanc Avatar answered Sep 21 '22 12:09

JPBlanc