Bird
0
0

You need to process a list of files but want the script to stop immediately if any file is missing. Which method ensures this behavior?

hard📝 Application Q8 of 15
PowerShell - Error Handling
You need to process a list of files but want the script to stop immediately if any file is missing. Which method ensures this behavior?
AUse Get-Item without error handling and rely on default behavior
BUse a try-catch block with Get-Item and -ErrorAction Stop for each file
CUse Get-Item with -ErrorAction SilentlyContinue and continue processing
DUse Write-Output to log missing files and proceed
Step-by-Step Solution
Solution:
  1. Step 1: Identify requirement

    The script must stop immediately if any file is missing.
  2. Step 2: Choose error handling

    Using -ErrorAction Stop causes terminating errors, which can be caught by try-catch to halt execution.
  3. Step 3: Evaluate options

    Use a try-catch block with Get-Item and -ErrorAction Stop for each file uses try-catch with -ErrorAction Stop, ensuring immediate stop on missing files.
  4. Final Answer:

    Use a try-catch block with Get-Item and -ErrorAction Stop for each file -> Option B
  5. Quick Check:

    Terminating errors + try-catch stop script [OK]
Quick Trick: Use -ErrorAction Stop with try-catch to halt [OK]
Common Mistakes:
  • Using -ErrorAction SilentlyContinue which ignores errors
  • Not using try-catch to handle terminating errors
  • Assuming default behavior stops script on missing files

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes