Bird
Raised Fist0
PowerShellscripting~20 mins

Custom error messages in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PowerShell Custom Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this PowerShell script with custom error?
Consider the following script that tries to divide two numbers and shows a custom error message if division by zero occurs. What will be the output?
PowerShell
try {
  $a = 10
  $b = 0
  $result = $a / $b
  Write-Output "Result: $result"
} catch {
  Write-Output "Error: Cannot divide by zero!"
}
ADivide by zero exception thrown, script stops
BResult: Infinity
CResult: 0
DError: Cannot divide by zero!
Attempts:
2 left
💡 Hint
Think about what happens when you divide by zero in PowerShell inside a try block.
💻 Command Output
intermediate
2:00remaining
What does this script output when input is invalid?
This script reads a number and throws a custom error if the input is not a number. What is the output if the user inputs 'abc'?
PowerShell
try {
  $input = 'abc'
  if (-not ($input -as [int])) {
    throw "Input is not a valid number"
  }
  Write-Output "Input is valid: $input"
} catch {
  Write-Output "Error: $_"
}
AError: Input is not a valid number
BError: System.Management.Automation.RuntimeException: Input is not a valid number
CInput is valid: abc
DNo output
Attempts:
2 left
💡 Hint
Check how the script throws and catches the error with a custom message.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a function that throws a custom error with a message?
You want to create a PowerShell function that throws a custom error with the message 'Invalid operation'. Which option is syntactically correct?
Afunction Test-Error { throw Invalid operation }
Bfunction Test-Error { throw "Invalid operation" }
Cfunction Test-Error { throw ('Invalid operation') }
Dfunction Test-Error { throw [string]'Invalid operation' }
Attempts:
2 left
💡 Hint
Remember how to throw a string error message in PowerShell.
🔧 Debug
advanced
2:00remaining
Why does this script not show the custom error message?
This script is supposed to catch an error and show a custom message, but it shows the default error instead. Why?
PowerShell
try {
  Write-Output "Start"
  throw "Something went wrong"
} catch {
  Write-Output $_.Exception.Message
}
ABecause Write-Output cannot print error messages
BBecause throw does not create an exception object
CBecause $_ is a string, not an object with Exception property
DBecause catch block is missing curly braces
Attempts:
2 left
💡 Hint
Check what $_ contains when throwing a string error.
🚀 Application
expert
3:00remaining
How to create a custom error object with a specific message and error ID?
You want to throw a custom error in PowerShell with message 'File not found' and error ID 'FileMissing'. Which code snippet correctly creates and throws this error?
A$err = New-Object System.Management.Automation.ErrorRecord (New-Object System.IO.FileNotFoundException 'File not found'), 'FileMissing', 'ObjectNotFound', $null; throw $err
Bthrow New-Object System.Management.Automation.ErrorRecord ('File not found', 'FileMissing', 'ObjectNotFound', $null)
Cthrow New-Object System.IO.FileNotFoundException('File not found', 'FileMissing')
D$err = New-Object System.Management.Automation.ErrorRecord ('File not found', 'FileMissing', 'ObjectNotFound', $null); throw $err
Attempts:
2 left
💡 Hint
ErrorRecord requires an Exception object as first argument.

Practice

(1/5)
1. What is the main purpose of using custom error messages in PowerShell scripts?
easy
A. To hide all errors from the user
B. To make the script run faster
C. To make error messages clearer and easier to understand for users
D. To automatically fix errors without user input

Solution

  1. Step 1: Understand the role of error messages

    Error messages help users know what went wrong in a script.
  2. Step 2: Identify the benefit of custom messages

    Custom messages explain errors in simple words, making them easier to fix.
  3. Final Answer:

    To make error messages clearer and easier to understand for users -> Option C
  4. Quick Check:

    Custom error messages improve clarity = A [OK]
Hint: Custom messages explain errors simply for users [OK]
Common Mistakes:
  • Thinking custom messages speed up scripts
  • Believing errors should be hidden completely
  • Assuming errors fix themselves automatically
2. Which of the following is the correct syntax to catch an error and display a custom message in PowerShell?
easy
A. try { command } finally { Write-Host 'Error occurred' }
B. catch { command } try { Write-Host 'Error occurred' }
C. try command catch Write-Host 'Error occurred'
D. try { command } catch { Write-Host 'Error occurred' }

Solution

  1. Step 1: Recall try-catch syntax

    PowerShell uses try { } catch { } blocks to handle errors.
  2. Step 2: Identify correct placement of Write-Host

    The custom message should be inside the catch block to show on error.
  3. Final Answer:

    try { command } catch { Write-Host 'Error occurred' } -> Option D
  4. Quick Check:

    try-catch with message in catch = A [OK]
Hint: Put custom message inside catch block [OK]
Common Mistakes:
  • Swapping try and catch blocks
  • Using finally instead of catch for errors
  • Missing curly braces around blocks
3. What will be the output of this script?
try {
  Get-Item 'C:\nonexistentfile.txt'
} catch {
  Write-Host 'File not found, please check the path.'
}
medium
A. Get-Item : Cannot find path 'C:\nonexistentfile.txt' because it does not exist.
B. File not found, please check the path.
C. No output, script runs silently
D. Syntax error in script

Solution

  1. Step 1: Understand Get-Item behavior

    Get-Item throws an error if the file does not exist.
  2. Step 2: Check catch block output

    The catch block catches the error and prints the custom message.
  3. Final Answer:

    File not found, please check the path. -> Option B
  4. Quick Check:

    Error caught and custom message shown = C [OK]
Hint: Catch block shows custom message on error [OK]
Common Mistakes:
  • Expecting default error instead of custom message
  • Thinking script runs silently without output
  • Confusing syntax error with runtime error
4. Identify the error in this script that tries to show a custom error message:
try {
  Get-Content 'missingfile.txt'
} catch {
  Write-Host Error: File missing
}
medium
A. Missing quotes around the error message string
B. Incorrect use of Get-Content command
C. No catch block present
D. Try block syntax is wrong

Solution

  1. Step 1: Check catch block syntax

    Write-Host requires the message to be a string inside quotes.
  2. Step 2: Identify missing quotes

    Error: File missing is not quoted, causing a syntax error.
  3. Final Answer:

    Missing quotes around the error message string -> Option A
  4. Quick Check:

    Strings need quotes in Write-Host = D [OK]
Hint: Always quote strings in Write-Host [OK]
Common Mistakes:
  • Forgetting quotes around text
  • Assuming Get-Content is wrong here
  • Thinking catch block is missing
5. You want to create a script that reads a file and shows a custom error message only if the file is missing, but lets other errors pass through. Which approach is best?
hard
A. Use try-catch and check if the error is 'ItemNotFoundException' before showing custom message
B. Use try-catch and always show the same custom message for any error
C. Use only try block without catch to let errors show normally
D. Use catch block to ignore all errors silently

Solution

  1. Step 1: Understand error filtering in catch

    PowerShell catch blocks can check error types to handle specific errors.
  2. Step 2: Apply condition for missing file error

    Check if error is 'ItemNotFoundException' to show custom message only for missing files.
  3. Final Answer:

    Use try-catch and check if the error is 'ItemNotFoundException' before showing custom message -> Option A
  4. Quick Check:

    Filter error type in catch for custom message = B [OK]
Hint: Check error type in catch to customize messages [OK]
Common Mistakes:
  • Showing same message for all errors
  • Ignoring errors silently
  • Not using catch block at all
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