Bird
Raised Fist0
PowerShellscripting~10 mins

Verbose and debug output in PowerShell - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Verbose and debug output
Start Script
Set VerbosePreference
Set DebugPreference
Run Commands
If Verbose enabled
Show Verbose Messages
If Debug enabled
Show Debug Messages
End Script
The script sets preferences for verbose and debug output, then runs commands that may produce messages shown only if those preferences are enabled.
Execution Sample
PowerShell
Write-Verbose "Starting script"
Write-Debug "Debug info"
Write-Output "Normal output"
This script writes verbose and debug messages plus normal output; verbose and debug messages show only if enabled.
Execution Table
StepCommandVerbosePreferenceDebugPreferenceVerbose OutputDebug OutputNormal Output
1Write-Verbose "Starting script"ContinueContinueStarting script
2Write-Debug "Debug info"ContinueContinueDebug info
3Write-Output "Normal output"ContinueContinueNormal output
4Write-Verbose "Starting script"SilentlyContinueSilentlyContinue
5Write-Debug "Debug info"SilentlyContinueSilentlyContinue
6Write-Output "Normal output"SilentlyContinueSilentlyContinueNormal output
7End of scriptSilentlyContinueSilentlyContinue
💡 Script ends after all commands run; verbose and debug outputs depend on preference settings.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
VerbosePreferenceContinueContinueContinueContinueSilentlyContinueSilentlyContinueSilentlyContinueSilentlyContinue
DebugPreferenceContinueContinueContinueContinueSilentlyContinueSilentlyContinueSilentlyContinueSilentlyContinue
Verbose OutputStarting scriptStarting scriptStarting script
Debug OutputDebug infoDebug info
Normal OutputNormal outputNormal outputNormal outputNormal outputNormal output
Key Moments - 3 Insights
Why do verbose messages sometimes not show even if Write-Verbose is used?
Verbose messages only show if VerbosePreference is set to 'Continue' or higher. See execution_table rows 1 and 4 where preference changes affect output.
What controls whether debug messages appear?
Debug messages appear only if DebugPreference is set to 'Continue'. In the table, rows 2 and 5 show debug output present or absent based on this.
Does Write-Output depend on Verbose or Debug preferences?
No, Write-Output always shows normal output regardless of VerbosePreference or DebugPreference, as seen in rows 3 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table row 2. What is the Debug Output at this step?
ANo output
BStarting script
CDebug info
DNormal output
💡 Hint
Check the 'Debug Output' column in row 2 of the execution_table.
At which step does the Verbose Output stop showing messages?
AStep 3
BStep 4
CStep 1
DStep 6
💡 Hint
Look at the VerbosePreference change and corresponding Verbose Output in rows 3 and 4.
If DebugPreference is set to 'SilentlyContinue', what happens to debug messages?
AThey do not show
BThey show normally
CThey show only once
DThey appear as errors
💡 Hint
Refer to Debug Output in rows 5 and 6 where DebugPreference is 'SilentlyContinue'.
Concept Snapshot
Write-Verbose and Write-Debug send messages shown only if VerbosePreference or DebugPreference is 'Continue'.
Set preferences with $VerbosePreference and $DebugPreference.
Write-Output always shows output.
Use verbose/debug to help see script details without cluttering normal output.
Full Transcript
This lesson shows how PowerShell scripts can produce extra messages for debugging or verbose info. The script sets preferences controlling if these messages appear. Write-Verbose messages show only if VerbosePreference is 'Continue'. Write-Debug messages show only if DebugPreference is 'Continue'. Normal output from Write-Output always shows. The execution table traces commands step-by-step, showing when messages appear or not depending on preferences. Variables track preference changes and outputs. Key moments clarify why messages may not appear and how preferences control them. The quiz tests understanding of when verbose and debug messages show. This helps beginners see how to add helpful script messages that can be turned on or off easily.

Practice

(1/5)
1. What is the main purpose of using Write-Verbose in a PowerShell script?
easy
A. To stop the script execution immediately
B. To write error messages to the console
C. To show extra informational messages when the script runs with -Verbose enabled
D. To create output files automatically

Solution

  1. Step 1: Understand Write-Verbose purpose

    Write-Verbose is used to add extra informational messages that only show when the script is run with the -Verbose flag.
  2. Step 2: Differentiate from other commands

    It does not stop execution, write errors, or create files. It only provides optional extra info.
  3. Final Answer:

    To show extra informational messages when the script runs with -Verbose enabled -> Option C
  4. Quick Check:

    Verbose messages = extra info shown with -Verbose [OK]
Hint: Verbose shows extra info only when -Verbose is used [OK]
Common Mistakes:
  • Confusing Write-Verbose with Write-Error
  • Expecting verbose messages without -Verbose flag
  • Thinking Write-Verbose stops script execution
2. Which of the following is the correct syntax to enable debug messages in a PowerShell script?
easy
A. Run the script with -Debug parameter
B. Add Write-Debug -Enable inside the script
C. Use Set-DebugMode On before running the script
D. Include Enable-Debug command in the script

Solution

  1. Step 1: Identify how to enable debug output

    Debug messages appear when the script is run with the -Debug parameter.
  2. Step 2: Check other options for validity

    There is no Write-Debug -Enable, Set-DebugMode, or Enable-Debug commands in PowerShell.
  3. Final Answer:

    Run the script with -Debug parameter -> Option A
  4. Quick Check:

    Debug enabled by running script with -Debug [OK]
Hint: Use -Debug flag when running script to see debug messages [OK]
Common Mistakes:
  • Trying to enable debug inside script with wrong commands
  • Confusing debug enabling with verbose enabling
  • Assuming debug is always on by default
3. What will be the output when running this script with -Verbose?
Write-Verbose "Starting process"
Write-Output "Process running"
Write-Verbose "Process completed"
medium
A. No output
B. Process running
C. Process running\nStarting process\nProcess completed
D. Starting process\nProcess running\nProcess completed

Solution

  1. Step 1: Understand Write-Verbose output with -Verbose

    When run with -Verbose, all Write-Verbose messages show along with normal output.
  2. Step 2: Identify output order

    The script writes verbose messages "Starting process" and "Process completed" plus the normal output "Process running" in order.
  3. Final Answer:

    Starting process Process running Process completed -> Option D
  4. Quick Check:

    Verbose + Output = all messages shown [OK]
Hint: Verbose messages show only with -Verbose, normal output always shows [OK]
Common Mistakes:
  • Ignoring verbose messages in output
  • Assuming verbose messages appear without -Verbose
  • Mixing order of output lines
4. You wrote a script using Write-Debug messages, but no debug output appears when running it with -Debug. What is the most likely cause?
medium
A. The debug preference variable $DebugPreference is set to 'SilentlyContinue'
B. The script does not contain any Write-Debug commands
C. The script was run with -Verbose instead of -Debug
D. The script has syntax errors preventing debug output

Solution

  1. Step 1: Check $DebugPreference effect

    Even with -Debug, if $DebugPreference is 'SilentlyContinue', debug messages won't show.
  2. Step 2: Verify other options

    The script does not contain any Write-Debug commands means no debug commands, but question states debug commands exist. The script was run with -Verbose instead of -Debug is about verbose, not debug. The script has syntax errors preventing debug output would cause errors, not silent debug.
  3. Final Answer:

    The debug preference variable $DebugPreference is set to 'SilentlyContinue' -> Option A
  4. Quick Check:

    $DebugPreference controls debug output display [OK]
Hint: Check $DebugPreference; must not be 'SilentlyContinue' to see debug [OK]
Common Mistakes:
  • Confusing -Verbose with -Debug flags
  • Ignoring $DebugPreference variable
  • Assuming debug always shows if -Debug used
5. You want to write a PowerShell script that shows detailed debug messages only when debugging is enabled, and verbose messages only when verbose is enabled. Which code snippet correctly implements this behavior?
hard
A.
$DebugPreference = 'Continue'
Write-Output "Normal output"
Write-Debug "Debug info"
Write-Verbose "Verbose info"
B.
Write-Debug "Debug info"
Write-Verbose "Verbose info"
Write-Output "Normal output"
C.
if ($DebugPreference -eq 'SilentlyContinue') { Write-Debug "Debug info" }
if ($VerbosePreference -eq 'SilentlyContinue') { Write-Verbose "Verbose info" }
Write-Output "Normal output"
D.
Write-Debug "Debug info"
Write-Verbose "Verbose info"
Write-Output "Normal output"
Set-Variable -Name DebugPreference -Value 'Continue'

Solution

  1. Step 1: Understand default behavior of Write-Debug and Write-Verbose

    These commands automatically show messages only if debugging or verbose is enabled by running script with -Debug or -Verbose.
  2. Step 2: Analyze code snippets

    Write-Debug "Debug info"
    Write-Verbose "Verbose info"
    Write-Output "Normal output"
    uses both commands directly, which is correct.
    $DebugPreference = 'Continue'
    Write-Output "Normal output"
    Write-Debug "Debug info"
    Write-Verbose "Verbose info"
    sets $DebugPreference to 'Continue' first, forcing debug messages to always show even without -Debug.
    if ($DebugPreference -eq 'SilentlyContinue') { Write-Debug "Debug info" }
    if ($VerbosePreference -eq 'SilentlyContinue') { Write-Verbose "Verbose info" }
    Write-Output "Normal output"
    checks for 'SilentlyContinue', showing messages when NOT enabled.
    Write-Debug "Debug info"
    Write-Verbose "Verbose info"
    Write-Output "Normal output"
    Set-Variable -Name DebugPreference -Value 'Continue'
    sets DebugPreference after messages, so ineffective.
  3. Final Answer:

    Write-Debug "Debug info" Write-Verbose "Verbose info" Write-Output "Normal output" -> Option B
  4. Quick Check:

    Write-Debug and Write-Verbose auto-check flags [OK]
Hint: Use Write-Debug and Write-Verbose directly; flags control output [OK]
Common Mistakes:
  • Forcing preference to 'Continue' unconditionally
  • Using incorrect condition like 'SilentlyContinue' in checks
  • Setting preferences after output commands