keepcalculator

06 Numbers and Powers

PowerShell Verbose & Debug Output: How to Use

Card No. 06·78927 · Updated · Standard formula, shown below

This guide shows you how to use PowerShell’s built-in verbose and debug streams to get more detail from your scripts and commands. You’ll learn how to write scripts that emit these streams, how to turn them on when you run a command, and how to control where the output goes. No prior knowledge beyond basic PowerShell usage is needed.

What You’ll Need

  • PowerShell 5.1 or later (most Windows 10/11 systems have this).
  • Windows PowerShell or PowerShell 7 (both work the same for these features).
  • A script or command you want to add verbose/debug output to.

Step-by-Step

  1. Understand the streams. PowerShell has six output streams: Success (normal output), Error, Warning, Verbose, Debug, and Information. Verbose and Debug are hidden by default; you must explicitly enable them to see them.
  2. Write verbose messages in your script. Use Write-Verbose "Your message here" to add detailed progress or diagnostic info. For example: Write-Verbose "Processing file $file". These messages are only shown when the -Verbose parameter is used.
  3. Write debug messages in your script. Use Write-Debug "Your debug message" for even more granular, developer-oriented details. For example: Write-Debug "Variable value: $var". These are shown when -Debug is used, and they trigger a prompt asking if you want to continue (unless $DebugPreference is set to Continue).
  4. Run a command with verbose output. For any cmdlet that supports it, add -Verbose to the command line. Example: Get-Process -Verbose. This shows the verbose stream from that cmdlet.
  5. Run a command with debug output. Add -Debug to the command line. Example: Get-Process -Debug. This shows debug messages and prompts you to continue after each one.
  6. Control the behavior with preference variables. Set $VerbosePreference and $DebugPreference to Continue to always show output, or SilentlyContinue to suppress it. For example: $VerbosePreference = 'Continue'. This affects all commands in the current session.
  7. Redirect verbose and debug streams to a file. Use 4>> verbose.log for verbose and 5>> debug.log for debug. Example: Get-Process -Verbose 4>> verbose.log. This appends the verbose stream to the file.
  8. Use [CmdletBinding()] to add -Verbose and -Debug to your own functions. At the top of your function, add [CmdletBinding()] before the param() block. This automatically gives your function the common parameters, including -Verbose and -Debug.
  9. Test your script with both switches. Run your script with -Verbose and then with -Debug to see the different levels of detail. Adjust your Write-Verbose and Write-Debug messages so they are useful at each level.

Common Mistakes

  • Using Write-Host instead of Write-Verbose. Write-Host always displays output and cannot be suppressed. Use Write-Verbose for optional detail.
  • Forgetting that -Verbose and -Debug are not available on all commands. They are only present on advanced functions (those with [CmdletBinding()]) and many built-in cmdlets. Simple scripts without that attribute won’t have them.
  • Not setting $DebugPreference when running non-interactively. If you run a script with -Debug in a scheduled task or CI pipeline, the prompt will hang. Set $DebugPreference = 'Continue' at the top of the script to avoid this.
  • Mixing up stream redirection numbers. Remember: 1 is success, 2 is error, 3 is warning, 4 is verbose, 5 is debug, and 6 is information. Using the wrong number will not capture the stream you want.

Tips

  • Use Write-Verbose for progress and Write-Debug for state. Verbose should describe what the script is doing (e.g., “Copying file…”), while Debug should reveal variable values and logic decisions (e.g., “File exists: True”).
  • Make verbose messages informative but not noisy. A good rule is: if a user running with -Verbose would be annoyed by the message, put it in Write-Debug instead.
  • Use the -Verbose parameter on your own functions by default. It costs nothing and makes your scripts much easier to troubleshoot.
  • Combine -Verbose with -ErrorAction to get more context on failures. For example, Get-Content -Verbose -ErrorAction Stop will show verbose output and stop on the first error.
  • In PowerShell 7, you can use 6>> for information stream, but verbose and debug are still 4 and 5. Keep this in mind if you’re writing cross-version scripts.

Troubleshooting

  • Verbose output is not showing even with -Verbose. Check if the command is a simple function or script without [CmdletBinding()]. Add [CmdletBinding()] to your function. Also check if $VerbosePreference is set to SilentlyContinue in your profile—override it with $VerbosePreference = 'Continue'.
  • Debug output prompts and hangs. This is normal when $DebugPreference is Inquire (the default). To avoid prompts, set $DebugPreference = 'Continue' at the start of your script or before running the command.
  • Redirection to a file creates an empty file. Make sure you’re using the correct stream number. For verbose, use 4>> and for debug, use 5>>. Also ensure the command actually produces verbose/debug output—some cmdlets are silent even with -Verbose.
  • Your own function doesn’t accept -Verbose. Add [CmdletBinding()] before param(). If you’re using a script file, also add [CmdletBinding()] to the script’s param() block.
  • Verbose output appears but is not formatted nicely. By default, verbose messages are prefixed with “VERBOSE:”. That’s normal. If you want to control formatting, use Write-Host with a custom color, but remember that’s not suppressible.

Frequently Asked Questions

  • What’s the difference between Write-Verbose and Write-Debug? Verbose is for high-level progress information that a user might want to see. Debug is for low-level details that help a developer diagnose issues. Debug also triggers a prompt by default, while verbose does not.
  • Can I use -Verbose on any cmdlet? No, only on advanced functions and cmdlets that explicitly support it. Most built-in cmdlets do, but simple scripts or functions without [CmdletBinding()] do not.
  • How do I make verbose output always show for a script? Set $VerbosePreference = 'Continue' at the top of the script. This will show all Write-Verbose messages without needing the -Verbose switch.
  • Can I redirect verbose output to a file without affecting normal output? Yes, use 4>> file.log. For example, Get-Process -Verbose 4>> verbose.log will only write the verbose stream to the file, while normal output goes to the console.
  • Why does -Debug ask me to continue? Because the default $DebugPreference is Inquire, which prompts on each debug message. Set $DebugPreference = 'Continue' to automatically continue without prompting.
  • Is there a way to see both verbose and debug at the same time? Yes, you can pass both -Verbose and -Debug to a command, or set both preference variables to Continue. The output will interleave, which can be noisy but gives full detail.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →