- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use the ErrorAction parameter in PowerShell?
Like ErrorActionPreference variable, ErrorAction parameter works similarly. ErrorAction parameter supported in Advance functions and most of the built-in cmdlets in PowerShell. It is useful to convert the non-terminating error to the terminating error and then you can handle them with try/catch blocks.
Supported Values and Examples,
Continue − This is the default value of the ErrorAction parameter and Error will be displayed and commands listed in Pipeline will be executed further.
Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist -ErrorAction Continue Write-Host "`nHello World" -BackgroundColor DarkGreen
Output
Get-WmiObject : The RPC server is unavailable. At line:1 char:1 + Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist -ErrorA ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMExcept ion + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands .GetWmiObjectCommand Hello World
Stop − Error message will be stopped displaying and the commands in the pipeline for execution won't run. There will be no output of the below example.
Example
PS C:\WINDOWS\system32>>> Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist -ErrorAction Stop Write-Host "`nHello World" -BackgroundColor DarkGreen
SilentlyContinue − An error message will not be displayed and the script executes the pipeline commands.
Example
Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist - ErrorAction SilentlyContinue Write-Host "`nHello World" -BackgroundColor DarkGreen
Output
PS C:\WINDOWS\system32>>> Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist -ErrorAction SilentlyContinue Write-Host "`nHello World" -BackgroundColor DarkGreen Hello World
Ignore − Ignore value is the same as the Silentlycontinue except the error output is not stored into $Error variable.
Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist -ErrorAction Ignore Write-Host "`nHello World" -BackgroundColor DarkGreen Hello World
Check the error variable now. You see in the below example, it won’t contain any error data while in the SilentlyContinue value, it stores the error output.
PS C:\WINDOWS\system32>>> $Error
Inquire − When an error occurred due to cmdlet, this option gives user below choices and prompt for appropriate action.
Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist - ErrorAction Inquire Write-Host "`nHello World" -BackgroundColor DarkGreen
Output
Confirm The RPC server is unavailable. [Y] Yes [A] Yes to All [H] Halt Command [S] Suspend [?] Help (default is " Y"):
An error message will be displayed if you select Yes/YestoAll and for Halt and suspend, the error won't be displayed.
Suspend − This value is used for the PowerShell workflow. Workflow is suspended for investigating error and then workflow can be resumed.
- Related Articles
- How to use an alias() for the parameter in PowerShell?
- How to use the -recursive parameter in Get-ChildItem using PowerShell?
- How to use –Wait parameter in Restart-Computer cmdlet in PowerShell?
- How to use -For parameter in Restart-Computer cmdlet in PowerShell?
- How to use –Timeout parameter in Restart-Computer cmdlet in PowerShell?
- What is the difference between $ErrorActionPreference and $ErrorAction cmdlet in PowerShell ?
- What is the use of Passthru parameter in Stop-Process in PowerShell?
- What is the use of -Force parameter in Get-ChildItem in PowerShell?
- How to validate the path with the PowerShell function parameter?
- How to copy folder contents in PowerShell with –Recurse parameter?
- How to use parameter/param arrays in C#?
- How to use stopwatch in PowerShell?
- How to use the ValidateLength attribute in PowerShell?
- How to use the ErrorActionPreference variable in PowerShell?
- How to use the tree command in PowerShell?
