Hi Guys, this is a quick blog post on something that I learned today on PowerShell.

Ashamed to say this… but I have never used -ErrorAction Ignore before, until today when I realized that one of the old scripts written by a colleague started to fail backward compatibility checks on Windows 7 / PowerShell v2.0. Within minutes into the google search, I found that it only works on PowerShell v3.0 and above, here is the article on Microsoft Docs for your reference.

So, there are a total of 6 action preferences, as you can see in the following screenshot after getting names in the enum: [System.Management.Automation.ActionPreference]

And usually, to suppress errors we use -ErrorAction SilentlyContinue that will not display any errors, but it will be added to $Error Automatic variable,  which is an array of error objects that represent the most recent errors occurred in PowerShell and can be used to check errors at a later stage of execution.

But, if I use -ErrorAction Ignore then, Errors are still suppressed but, now they are not added to the $Error Array variable. This can come very handy when we want to exclude errors thrown by specific cmdlets from the automatic variable: $Error . Following are the commands and screenshot for your reference:


-ErrorAction Ignore

 

Optical Character Recognition
~ Author of “PowerShell Guide to Python“,  and currently writing a Book on “Windows Subsystem for Linux (WSL)


All my books are available as a discounted bundle:

  1. PowerShell Guide to Python : This PowerShell Scripting guide to Python is designed to make readers familiar with syntax, semantics and core concepts of Python language, in an approach that readers can totally relate with the concepts of PowerShell already in their arsenal, to learn Python fast and effectively, such that it sticks with readers for longer time.
  2. Windows Subsystem for Linux (WSL) : Keywords, definitions, and problems WSL solve and how it works under the hoods. From download to setup to interoperability this book even covers details like the architecture of Windows subsystem for Linux and new features in WSL 2 with some wonderful use cases.

 


Subscribe to our mailing list

* indicates required