Challenge - 5 Problems
PowerShell Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output when error handling is used?
Consider this PowerShell script that tries to read a file. What will be the output when the file does not exist?
PowerShell
try {
Get-Content -Path 'nonexistentfile.txt' -ErrorAction Stop
Write-Output 'File read successfully'
} catch {
Write-Output 'Error caught: File not found'
}Attempts:
2 left
💡 Hint
Think about what happens when the file is missing and how the catch block works.
✗ Incorrect
The try block attempts to read a file that does not exist, which causes an error. The catch block catches this error and outputs a friendly message instead of letting the script fail.
🧠 Conceptual
intermediate1:30remaining
Why use error handling in scripts?
Why is it important to use error handling like try-catch in scripts?
Attempts:
2 left
💡 Hint
Think about what happens if errors are not handled.
✗ Incorrect
Error handling lets the script catch problems and respond without crashing, so the script can continue or provide useful feedback.
🔧 Debug
advanced2:30remaining
Identify the error handling mistake
This script is supposed to catch errors but fails to do so. What is the mistake?
PowerShell
try {
Get-Content -Path 'missingfile.txt' -ErrorAction SilentlyContinue
Write-Output 'File read'
} catch {
Write-Output 'Error caught'
}Attempts:
2 left
💡 Hint
Check how -ErrorAction affects error handling.
✗ Incorrect
Using -ErrorAction SilentlyContinue tells PowerShell to suppress errors, so the catch block never runs because no terminating error occurs.
💻 Command Output
advanced2:00remaining
What happens without error handling?
What will be the output of this script when the file does not exist?
PowerShell
Get-Content -Path 'nofile.txt' Write-Output 'This line runs after file read'
Attempts:
2 left
💡 Hint
Think about what happens when an error is not caught.
✗ Incorrect
Without error handling, Get-Content writes a non-terminating error message but continues execution, so the next line runs.
🚀 Application
expert3:00remaining
How to ensure script continues after error?
You want your PowerShell script to try reading a file and if it fails, print a message but continue running other commands. Which script achieves this?
Attempts:
2 left
💡 Hint
Consider how to make errors terminating to trigger catch and continue script.
✗ Incorrect
Option C uses -ErrorAction Stop to make errors terminating so catch runs, allowing the script to handle the error and continue.