0
0
PowerShellscripting~10 mins

ErrorAction parameter in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ErrorAction parameter
Run Command
Error Occurs?
NoContinue Normal Execution
Yes
Check ErrorAction Value
SilentlyContinue
Ignore
Next Step Based on Action
When a command runs, if an error happens, PowerShell checks the ErrorAction parameter to decide what to do next.
Execution Sample
PowerShell
Get-Item 'C:\no_such_file.txt' -ErrorAction SilentlyContinue
Write-Output 'Script continues'
Try to get a file that does not exist, but ignore the error and continue running the script.
Execution Table
StepCommandError Occurs?ErrorActionAction TakenOutput
1Get-Item 'C:\no_such_file.txt' -ErrorAction SilentlyContinueYesSilentlyContinueIgnore error, no message
2Write-Output 'Script continues'NoN/APrint messageScript continues
3End of scriptN/AN/AScript ends normally
💡 Script ends normally after ignoring the error due to SilentlyContinue
Variable Tracker
VariableStartAfter Step 1After Step 2Final
ErrorNoneError occurred but ignoredNoneNone
OutputNoneNoneScript continuesScript continues
Key Moments - 2 Insights
Why does the script continue even though Get-Item fails?
Because ErrorAction is set to SilentlyContinue, the error is ignored and no message is shown, as seen in execution_table step 1.
What happens if ErrorAction is set to Stop?
The script would stop immediately on error, not reaching step 2, unlike the SilentlyContinue case in the table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 1?
ANo output, error ignored
BError message shown
CScript stops
DOutput 'Script continues'
💡 Hint
Check the 'Output' column in row for step 1 in execution_table
At which step does the script print 'Script continues'?
AStep 1
BStep 3
CStep 2
DNever
💡 Hint
Look at the 'Output' column in execution_table for step 2
If ErrorAction was changed to Stop, what would happen at step 2?
AStep 2 runs normally
BStep 2 is skipped because script stops
CError is ignored silently
DScript asks user what to do
💡 Hint
Refer to key_moments explanation about ErrorAction Stop behavior
Concept Snapshot
ErrorAction parameter controls what PowerShell does when a command errors.
Common values:
- SilentlyContinue: ignore error, no message
- Continue: show error, keep running
- Stop: stop script immediately
- Inquire: ask user what to do
Use it to control error handling behavior in scripts.
Full Transcript
The ErrorAction parameter in PowerShell tells the script what to do if a command causes an error. When a command runs, PowerShell checks if an error happens. If no error, the script continues normally. If there is an error, PowerShell looks at the ErrorAction value. If it is SilentlyContinue, the error is ignored and no message shows. The script keeps running. If it is Continue, the error message shows but the script keeps running. If it is Stop, the script stops immediately. If it is Inquire, PowerShell asks the user what to do. In the example, Get-Item tries to get a file that does not exist. Because ErrorAction is SilentlyContinue, the error is ignored and the script prints 'Script continues'. This shows how ErrorAction controls error handling in scripts.