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
{
}
Share:
21,002
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    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
    HouseCat about 9 years
    This 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
    cjones26 almost 8 years
    Excellent comment, @RagingCain!! This was driving me nuts.
  • Hairy
    Hairy over 5 years
    Worked well for me too, great stuff.
  • JamesQMurphy
    JamesQMurphy over 5 years
    Note 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
    johny why almost 3 years
    @JamesQMurphy Just for that powershell session? Or all future sessions?
  • JamesQMurphy
    JamesQMurphy almost 3 years
    @johnywhy Just for that session