Bird
Raised Fist0
PowerShellscripting~20 mins

ErrorAction parameter in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PowerShell ErrorAction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output when using ErrorAction SilentlyContinue?
Consider the following PowerShell command that tries to get a non-existent service with ErrorAction set to SilentlyContinue. What will be the output?
PowerShell
Get-Service -Name 'NoSuchService' -ErrorAction SilentlyContinue
Write-Output 'Command completed'
ANo output from Get-Service, then 'Command completed' printed
BError message about service not found, then 'Command completed' printed
CScript stops with an error and does not print 'Command completed'
DEmpty output and no 'Command completed' printed
Attempts:
2 left
💡 Hint
ErrorAction SilentlyContinue suppresses error messages but lets the script continue.
💻 Command Output
intermediate
2:00remaining
What happens with ErrorAction Stop on a failing command?
What is the result of running this PowerShell command that tries to get a non-existent service with ErrorAction set to Stop? Get-Service -Name 'NoSuchService' -ErrorAction Stop Write-Output 'This line runs'
PowerShell
Get-Service -Name 'NoSuchService' -ErrorAction Stop
Write-Output 'This line runs'
AError message printed but script continues to print 'This line runs'
BError is ignored and 'This line runs' is printed
CError is thrown and script stops before printing 'This line runs'
DNo error, empty output, and 'This line runs' printed
Attempts:
2 left
💡 Hint
ErrorAction Stop causes the script to stop on errors.
📝 Syntax
advanced
2:00remaining
Which command correctly uses ErrorAction to ignore errors?
Select the PowerShell command that correctly uses the ErrorAction parameter to ignore errors when deleting a file that may not exist.
ARemove-Item 'C:\temp\file.txt' -ErrorAction Ignore
BRemove-Item 'C:\temp\file.txt' -ErrorAction SilentlyContinue
CRemove-Item 'C:\temp\file.txt' -ErrorAction Continue
DRemove-Item 'C:\temp\file.txt' -ErrorAction Stop
Attempts:
2 left
💡 Hint
ErrorAction SilentlyContinue suppresses error messages and continues.
🚀 Application
advanced
2:00remaining
How to catch errors when using ErrorAction Stop?
You want to run a command that stops on error and catch that error to handle it gracefully. Which script snippet correctly does this?
Atry { Get-Item 'C:\no_file.txt' } catch { Write-Output 'File not found' }
BGet-Item 'C:\no_file.txt' -ErrorAction Stop; if ($?) { Write-Output 'File found' } else { Write-Output 'File not found' }
CGet-Item 'C:\no_file.txt' -ErrorAction SilentlyContinue; Write-Output 'Checked file'
Dtry { Get-Item 'C:\no_file.txt' -ErrorAction Stop } catch { Write-Output 'File not found' }
Attempts:
2 left
💡 Hint
Use try/catch with ErrorAction Stop to catch terminating errors.
🧠 Conceptual
expert
2:00remaining
What is the difference between ErrorAction SilentlyContinue and Ignore?
In PowerShell, what is the difference between using ErrorAction SilentlyContinue and ErrorAction Ignore?
AIgnore is not a valid ErrorAction value and causes an error; SilentlyContinue suppresses errors
BSilentlyContinue and Ignore are synonyms and behave the same
CSilentlyContinue suppresses error messages but runs error handlers; Ignore suppresses errors completely and skips error handlers
DIgnore logs errors to a file; SilentlyContinue shows errors on screen
Attempts:
2 left
💡 Hint
Check PowerShell documentation for valid ErrorAction values.

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

  1. Step 1: Understand the SilentlyContinue behavior

    The SilentlyContinue value tells PowerShell to hide error messages and keep running the script without interruption.
  2. Step 2: Compare with other ErrorAction values

    Unlike Stop, which halts the script, SilentlyContinue ignores errors silently, so the script continues smoothly.
  3. Final Answer:

    It suppresses error messages and continues running the script. -> Option C
  4. 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

  1. Step 1: Identify the ErrorAction value that stops script

    The Stop value causes the script to halt immediately when an error occurs.
  2. Step 2: Verify syntax correctness

    The syntax uses -ErrorAction Stop correctly after the command.
  3. Final Answer:

    Get-Item 'C:\file.txt' -ErrorAction Stop -> Option A
  4. 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?
Get-Item 'C:\nonexistentfile.txt' -ErrorAction SilentlyContinue; Write-Output 'Done'
medium
A. Only 'Done' is printed, no error message
B. Error message about missing file, then 'Done'
C. Script stops with error, no output
D. Nothing is printed

Solution

  1. Step 1: Analyze Get-Item with SilentlyContinue

    The command tries to get a missing file but errors are suppressed silently.
  2. Step 2: Check what Write-Output does after

    Since error is suppressed, the script continues and prints 'Done'.
  3. Final Answer:

    Only 'Done' is printed, no error message -> Option A
  4. Quick Check:

    SilentlyContinue hides errors, script continues [OK]
Hint: SilentlyContinue hides errors, next commands run [OK]
Common Mistakes:
  • Expecting error message to show
  • Thinking script stops on error
  • Assuming no output at all
4. 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?
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

  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]
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

  1. Step 1: Understand SilentlyContinue hides all errors

    Using SilentlyContinue alone skips errors silently, so permission errors won't stop script.
  2. 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.
  3. Final Answer:

    Use Remove-Item -ErrorAction SilentlyContinue and check error type manually -> Option D
  4. Quick Check:

    SilentlyContinue + manual check = skip missing, stop permission errors [OK]
Hint: SilentlyContinue plus manual error check handles complex cases [OK]
Common Mistakes:
  • Using Stop stops on all errors including missing files
  • Using Continue shows errors but does not stop script
  • Ignoring error type and blindly continuing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard