Bird
0
0

How can you design a PowerShell script to display verbose messages only when -Verbose is used, and debug messages only when -Debug is used, without mixing outputs?

hard📝 Application Q8 of 15
PowerShell - Scripting Best Practices
How can you design a PowerShell script to display verbose messages only when -Verbose is used, and debug messages only when -Debug is used, without mixing outputs?
AUse <code>Write-Host</code> with conditional checks on $VerbosePreference and $DebugPreference
BUse <code>Write-Output</code> for all messages and filter them manually
CUse <code>Write-Verbose</code> for debug messages and <code>Write-Debug</code> for verbose messages
DUse <code>Write-Verbose</code> for verbose messages and <code>Write-Debug</code> for debug messages, relying on the built-in parameters
Step-by-Step Solution
Solution:
  1. Step 1: Understand cmdlet purposes

    Write-Verbose outputs verbose messages; Write-Debug outputs debug messages.
  2. Step 2: Use built-in parameters

    PowerShell automatically controls display of these messages with -Verbose and -Debug switches.
  3. Step 3: Avoid manual filtering

    Using Write-Output or Write-Host with manual checks complicates the script and is error-prone.
  4. Final Answer:

    Use Write-Verbose for verbose messages and Write-Debug for debug messages, relying on the built-in parameters -> Option D
  5. Quick Check:

    Write-Verbose and Write-Debug respect -Verbose and -Debug flags [OK]
Quick Trick: Use Write-Verbose and Write-Debug with respective flags [OK]
Common Mistakes:
  • Mixing Write-Verbose and Write-Debug usage
  • Using Write-Output for debug/verbose messages
  • Manually filtering messages instead of using built-in features

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes