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:
~ 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:
- 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.
- 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.
Prateek Singh
Related posts
1 Comment
Leave a ReplyCancel reply
Categories
Author of Books
Awards
Open Sourced Projects
Author at
Blog Roll
Mike F RobbinsDamien Van Robaeys
Stéphane van Gulick
Kevin Marquette
Adam Bertram
Stephanos Constantinou
Francois-Xavier Cat
Ravikanth Chaganti
Roman Levchenko
Blog Stats
- 1,132,410 People Reached
Nice catch Prateek, thanks for sharing m8