Bird
Raised Fist0
PowerShellscripting~10 mins

Terminating vs non-terminating errors in PowerShell - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Concept Flow - Terminating vs non-terminating errors
Run Command
↓
Error Occurs?
No→Continue Execution
Yes↓
Is Error Terminating?
No→Show Error, Continue
Yes↓
Stop Execution Immediately
The script runs a command, checks if an error occurs, then decides if it stops or continues based on error type.
Execution Sample
PowerShell
Get-Item 'C:\NoSuchFile.txt'
Write-Output 'This runs after non-terminating error'

Get-Item 'C:\NoSuchFile.txt' -ErrorAction Stop
Write-Output 'This does NOT run after terminating error'
Shows a non-terminating error that allows script to continue, then a terminating error that stops the script.
Execution Table
StepCommandError Occurs?Error TypeAction TakenOutput
1Get-Item 'C:\NoSuchFile.txt'YesNon-terminatingShow error, continueError message displayed
2Write-Output 'This runs after non-terminating error'NoN/ARuns normallyThis runs after non-terminating error
3Get-Item 'C:\NoSuchFile.txt' -ErrorAction StopYesTerminatingStop executionError message displayed
4Write-Output 'This does NOT run after terminating error'NoN/ADoes NOT run
💡 Execution stops at step 3 due to terminating error triggered by -ErrorAction Stop
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
ErrorStatenullNon-terminating error recordedCleared after outputTerminating error triggeredScript stopped
Key Moments - 3 Insights
Why does the script continue after the first Get-Item command even though an error occurs?
Because the error is non-terminating by default, PowerShell shows the error but continues running the next commands as shown in execution_table step 1 and 2.
What causes the script to stop after the second Get-Item command?
The -ErrorAction Stop parameter makes the error terminating, so PowerShell stops execution immediately as shown in execution_table step 3.
Does the Write-Output command after the terminating error run?
No, it does not run because the script stops at the terminating error, as shown in execution_table step 4 where output is empty.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what type of error occurs at step 1?
ANon-terminating error
BTerminating error
CNo error
DSyntax error
💡 Hint
Check the 'Error Type' column in execution_table row for step 1
At which step does the script stop running commands?
AStep 2
BStep 4
CStep 3
DScript never stops
💡 Hint
Look at the 'Action Taken' and 'Output' columns in execution_table for step 3 and 4
If the -ErrorAction Stop was removed from step 3, what would happen?
AScript would stop at step 3
BScript would continue to step 4
CNo error would occur
DScript would crash
💡 Hint
Refer to how non-terminating errors behave in execution_table steps 1 and 2
Concept Snapshot
Terminating errors stop script immediately.
Non-terminating errors show message but continue.
Use -ErrorAction Stop to make errors terminating.
Default errors are non-terminating.
Check error type to control script flow.
Full Transcript
This lesson shows the difference between terminating and non-terminating errors in PowerShell. When a command causes a non-terminating error, PowerShell shows the error but keeps running the script. When a terminating error happens, PowerShell stops the script immediately. Using -ErrorAction Stop forces an error to be terminating. The example runs Get-Item on a missing file twice: first without stopping, then with stopping. The output and script flow change accordingly.

Practice

(1/5)
1. What is the main difference between terminating and non-terminating errors in PowerShell?
easy
A. Neither error affects script execution.
B. Terminating errors stop the script execution, non-terminating errors allow it to continue.
C. Both errors stop the script execution immediately.
D. Non-terminating errors stop the script execution, terminating errors allow it to continue.

Solution

  1. Step 1: Understand error types in PowerShell

    Terminating errors cause the script to stop unless handled, while non-terminating errors only show warnings.
  2. Step 2: Compare effects on script execution

    Terminating errors halt the script, non-terminating errors let it continue running.
  3. Final Answer:

    Terminating errors stop the script execution, non-terminating errors allow it to continue. -> Option B
  4. Quick Check:

    Terminating stops, non-terminating continues [OK]
Hint: Terminating errors stop scripts; non-terminating just warn [OK]
Common Mistakes:
  • Confusing which error type stops the script
  • Thinking non-terminating errors halt execution
  • Assuming both errors behave the same
2. Which of the following is the correct way to make a non-terminating error behave like a terminating error in PowerShell?
easy
A. Use the parameter -ErrorAction Stop
B. Use the parameter -ErrorAction Continue
C. Use the parameter -ErrorAction SilentlyContinue
D. Use the parameter -ErrorAction Ignore

Solution

  1. Step 1: Recall the -ErrorAction parameter options

    The -ErrorAction parameter controls how errors behave; Stop makes non-terminating errors act as terminating.
  2. Step 2: Match the correct option

    -ErrorAction Stop forces the error to stop the script, unlike Continue or SilentlyContinue which allow continuation.
  3. Final Answer:

    Use the parameter -ErrorAction Stop -> Option A
  4. Quick Check:

    -ErrorAction Stop makes errors terminate [OK]
Hint: Use -ErrorAction Stop to force termination on errors [OK]
Common Mistakes:
  • Using Continue or SilentlyContinue expecting termination
  • Confusing Ignore with Stop
  • Not knowing -ErrorAction controls error behavior
3. What will be the output of this PowerShell script?
Get-Item 'C:\nonexistentfile.txt' -ErrorAction Continue
Write-Output 'Script continues'
medium
A. No error message, only 'Script continues' is printed
B. Script stops with an error, 'Script continues' is not printed
C. Error message shown, then 'Script continues' is printed
D. Script crashes with no output

Solution

  1. Step 1: Understand -ErrorAction Continue behavior

    With -ErrorAction Continue, non-terminating errors show error messages but do not stop the script.
  2. Step 2: Trace script output

    The error about missing file is displayed, then the script prints 'Script continues'.
  3. Final Answer:

    Error message shown, then 'Script continues' is printed -> Option C
  4. Quick Check:

    Continue shows error but script runs [OK]
Hint: Continue shows error but script keeps running [OK]
Common Mistakes:
  • Assuming script stops on Continue error
  • Expecting no error message with Continue
  • Confusing Continue with Stop
4. You run this script but it continues unexpectedly:
Try {
  Get-Item 'C:\nonexistentfile.txt' -ErrorAction Stop
} Catch {
  Write-Output 'Caught error'
}
Write-Output 'Script continues'

What is the likely cause?
medium
A. The script stops because the Catch block is missing
B. The error is non-terminating, so Try/Catch does not catch it
C. The script stops because -ErrorAction Stop causes termination without Catch
D. The error is terminating and caught, so script continues after Catch

Solution

  1. Step 1: Analyze -ErrorAction Stop with Try/Catch

    Using -ErrorAction Stop makes the error terminating, so Try/Catch can catch it.
  2. Step 2: Understand script flow after Catch

    The Catch block runs and outputs 'Caught error', then script continues to print 'Script continues'.
  3. Final Answer:

    The error is terminating and caught, so script continues after Catch -> Option D
  4. Quick Check:

    Terminating error caught, script continues [OK]
Hint: Try/Catch catches terminating errors with -ErrorAction Stop [OK]
Common Mistakes:
  • Thinking non-terminating errors trigger Catch
  • Assuming script stops without Catch block
  • Confusing error action effects on Try/Catch
5. You want to run a script that tries to get multiple files, but you want it to stop immediately if any file is missing. Which approach correctly enforces this behavior?
hard
A. Use Get-Item with -ErrorAction Stop inside a Try/Catch block to handle errors.
B. Use Get-Item with -ErrorAction Continue and no error handling.
C. Use Get-Item with -ErrorAction SilentlyContinue and log errors later.
D. Use Get-Item without any -ErrorAction parameter.

Solution

  1. Step 1: Identify requirement to stop on missing file

    Stopping immediately on error means using terminating errors with -ErrorAction Stop.
  2. Step 2: Use Try/Catch to handle and control flow

    Wrapping calls in Try/Catch lets you catch the error and stop or handle it as needed.
  3. Final Answer:

    Use Get-Item with -ErrorAction Stop inside a Try/Catch block to handle errors. -> Option A
  4. Quick Check:

    Stop errors + Try/Catch = immediate stop [OK]
Hint: Use -ErrorAction Stop with Try/Catch to halt on errors [OK]
Common Mistakes:
  • Using Continue or SilentlyContinue expecting stop
  • Not using Try/Catch to handle terminating errors
  • Ignoring error handling for critical failures
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