PowerShell: ErrorAction set to "SilentlyContinue" not working
21,002
Solution 1
Add this first.
$ErrorActionPreference = "silentlycontinue"
Solution 2
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
{
}
Author by
Admin
Updated on July 09, 2022Comments
-
Admin almost 2 years
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?
-
HouseCat about 9 yearsThis may need to be set in addition to a try catch due to objects (outside of your code) having exceptions being thrown back at your session. By setting the global preference, you also silence these errors being reported to screen.
-
cjones26 almost 8 yearsExcellent comment, @RagingCain!! This was driving me nuts.
-
Hairy over 5 yearsWorked well for me too, great stuff.
-
JamesQMurphy over 5 yearsNote that this sets the ErrorAction preference for all future calls as well. If you only want this set for one call (or a small handful of calls), then you need to set it back to whatever it was before.
-
johny why almost 3 years@JamesQMurphy Just for that powershell session? Or all future sessions?
-
JamesQMurphy almost 3 years@johnywhy Just for that session