When my PowerShell script tries, for example, to create a SQL Server object for a server that doesn't exist ("bla" in my case), PowerShell displays lots of PowerShell errors in red.
Since my script checks the value of $?
after such calls, and displays and logs errors, I'd rather not have the several lines of PowerShell errors displayed as well.
How can I deactivate those being displayed for my script?
You have a couple of options. The easiest involve using the ErrorAction
settings.
-Erroraction
is a universal parameter for all cmdlets. If there are special commands you want to ignore you can use -erroraction 'silentlycontinue'
which will basically ignore all error messages generated by that command. You can also use the Ignore
value (in PowerShell 3+):
Unlike SilentlyContinue, Ignore does not add the error message to the $Error automatic variable.
If you want to ignore all errors in a script, you can use the system variable $ErrorActionPreference
and do the same thing: $ErrorActionPreference= 'silentlycontinue'
See about_CommonParameters for more info about -ErrorAction. See about_preference_variables for more info about $ErrorActionPreference.
do you need single quote in -erroraction 'silentlycontinue'? Intellisese is showing options and not adding single quote.
If the format above doesn't work for you, reference the link provided above. I had to format as
-ErrorAction:SilentlyContinue
in PS 5.1. I was calling my cmdlet from batch so I don't know if that makes a difference. But good info when you know an acceptable error may be thrown.--rm .\Windows.old\ -Force -Recurse -Verbose -ErrorAction SilentlyContinue -WarningAction SilentlyContinue </code>