Bird
0
0

You wrote this script:

medium📝 Debug Q14 of 15
PowerShell - Error Handling
You wrote this script:
Get-Content 'C:\missing.txt' -ErrorAction Stop
Write-Output 'File read successfully'

But the script does not print 'File read successfully' when the file is missing. What is the fix?
AChange -ErrorAction Stop to -ErrorAction SilentlyContinue
BAdd try/catch block around Get-Content
CRemove the Write-Output line
DChange -ErrorAction Stop to -ErrorAction Continue
Step-by-Step Solution
Solution:
  1. Step 1: Understand Stop behavior

    -ErrorAction Stop halts script on error, so Write-Output is not reached if file missing.
  2. Step 2: Use try/catch to handle error gracefully

    Wrapping Get-Content in try/catch lets you catch the error and continue script safely.
  3. Final Answer:

    Add try/catch block around Get-Content -> Option B
  4. Quick Check:

    Stop halts script; try/catch handles errors [OK]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes