What is the purpose of the ErrorAction parameter in PowerShell?
The ErrorAction parameter controls how PowerShell responds to non-terminating errors during command execution. It lets you decide whether to continue, stop, silently ignore, or ask for user input when an error occurs.
Click to reveal answer
beginner
Name the common values you can assign to the ErrorAction parameter.
Common values include: - Continue: Show error and continue (default) - Stop: Stop execution on error - SilentlyContinue: Ignore error without message - Inquire: Ask user what to do - Ignore: Ignore error and don't add to error stream
Click to reveal answer
beginner
How do you use ErrorAction to stop a script when a command fails?
Add -ErrorAction Stop to the command. This makes PowerShell treat non-terminating errors as terminating, so the script stops immediately on error.
Click to reveal answer
beginner
What happens if you set ErrorAction to SilentlyContinue?
PowerShell ignores the error and does not display any message. The script continues running as if no error happened.
Click to reveal answer
beginner
Give an example of a PowerShell command using ErrorAction to silently ignore errors.
Example: Get-Item 'C:\nonexistentfile.txt' -ErrorAction SilentlyContinue This command tries to get a file that doesn't exist but won't show an error message.
Click to reveal answer
What does -ErrorAction Stop do in a PowerShell command?
AAsks the user what to do
BIgnores the error silently
CContinues and shows the error message
DStops the script if an error occurs
✗ Incorrect
-ErrorAction Stop makes PowerShell treat errors as terminating, stopping the script immediately.
Which ErrorAction value will NOT display any error message but continue running?
ASilentlyContinue
BStop
CContinue
DInquire
✗ Incorrect
SilentlyContinue ignores errors without showing messages.
If you want PowerShell to ask you what to do when an error happens, which ErrorAction value should you use?
AIgnore
BInquire
CStop
DContinue
✗ Incorrect
Inquire prompts the user to decide how to handle the error.
What is the default behavior of PowerShell when an error occurs and ErrorAction is not specified?
AStop execution
BSilently ignore error
CContinue and show error message
DAsk user for input
✗ Incorrect
By default, PowerShell continues running and shows the error message (Continue).
Which ErrorAction value completely ignores the error and does not add it to the error stream?
AIgnore
BSilentlyContinue
CStop
DContinue
✗ Incorrect
Ignore suppresses the error and does not add it to the error stream, unlike SilentlyContinue.
Explain how the ErrorAction parameter changes the way PowerShell handles errors in a script.
Think about how you want your script to behave when something goes wrong.
You got /4 concepts.
Describe a situation where using -ErrorAction SilentlyContinue would be helpful.
Imagine you check for a file that might not exist, but it’s okay if it doesn’t.
You got /3 concepts.
Practice
(1/5)
1. What does the -ErrorAction SilentlyContinue parameter do in a PowerShell command?
easy
A. It displays error messages and pauses the script.
B. It stops the script immediately when an error occurs.
C. It suppresses error messages and continues running the script.
D. It logs errors to a file but shows no messages.
Solution
Step 1: Understand the SilentlyContinue behavior
The SilentlyContinue value tells PowerShell to hide error messages and keep running the script without interruption.
Step 2: Compare with other ErrorAction values
Unlike Stop, which halts the script, SilentlyContinue ignores errors silently, so the script continues smoothly.
Final Answer:
It suppresses error messages and continues running the script. -> Option C
Quick Check:
SilentlyContinue = suppress errors and continue [OK]
Hint: SilentlyContinue hides errors and keeps script running [OK]
Common Mistakes:
Confusing SilentlyContinue with Stop which halts execution
Thinking errors are logged automatically
Assuming errors still display on screen
2. Which of the following is the correct syntax to stop a PowerShell script on error using the ErrorAction parameter?
easy
A. Get-Item 'C:\file.txt' -ErrorAction Stop
B. Get-Item 'C:\file.txt' -ErrorAction Ignore
C. Get-Item 'C:\file.txt' -ErrorAction SilentlyContinue
D. Get-Item 'C:\file.txt' -ErrorAction Continue
Solution
Step 1: Identify the ErrorAction value that stops script
The Stop value causes the script to halt immediately when an error occurs.
Step 2: Verify syntax correctness
The syntax uses -ErrorAction Stop correctly after the command.
Final Answer:
Get-Item 'C:\file.txt' -ErrorAction Stop -> Option A
Quick Check:
ErrorAction Stop halts script on error [OK]
Hint: Use -ErrorAction Stop to halt script on errors [OK]
Common Mistakes:
Using Continue which only shows errors but continues
Using SilentlyContinue which hides errors but continues
Using non-existent value Ignore
3. What will be the output of this PowerShell command?
But the script does not print 'File read successfully' when the file is missing. What is the fix?
medium
A. Change -ErrorAction Stop to -ErrorAction SilentlyContinue
B. Add try/catch block around Get-Content
C. Remove the Write-Output line
D. Change -ErrorAction Stop to -ErrorAction Continue
Solution
Step 1: Understand Stop behavior
-ErrorAction Stop halts script on error, so Write-Output is not reached if file missing.
Step 2: Use try/catch to handle error gracefully
Wrapping Get-Content in try/catch lets you catch the error and continue script safely.
Final Answer:
Add try/catch block around Get-Content -> Option B
Quick Check:
Stop halts script; try/catch handles errors [OK]
Hint: Use try/catch with Stop to handle errors safely [OK]
Common Mistakes:
Changing Stop to SilentlyContinue hides errors but may ignore important failures
Removing Write-Output loses useful output
Using Continue still shows errors but doesn't stop script
5. You want to run a script that tries to delete multiple files. If a file is missing, you want to skip it silently and continue deleting others. But if a file exists but cannot be deleted due to permission issues, the script should stop immediately. Which ErrorAction usage fits this requirement?
hard
A. Use Remove-Item -ErrorAction Continue and handle errors in catch block
B. Use Remove-Item -ErrorAction Stop for all files
C. Use Remove-Item -ErrorAction SilentlyContinue for all files
D. Use Remove-Item -ErrorAction SilentlyContinue and check error type manually
Solution
Step 1: Understand SilentlyContinue hides all errors
Using SilentlyContinue alone skips errors silently, so permission errors won't stop script.
Step 2: Combine SilentlyContinue with manual error check
Run Remove-Item with SilentlyContinue, then check error details to stop on permission errors but ignore missing files.
Final Answer:
Use Remove-Item -ErrorAction SilentlyContinue and check error type manually -> Option D