Write Debug - PowerShell


Overview

The Write-Debug cmdlet outputs debug messages to the debug stream. These messages can be helpful for troubleshooting scripts and understanding the flow of execution.

Syntax

Write-Debug [-Message] <string> [[-Category] <string>] [[-Source] <string>]

Options/Flags

  • -Message: (Required) Specifies the debug message to output.
  • -Category: (Optional) Specifies the category of the debug message. Valid categories are:
    • Default (default)
    • Error
    • Warning
    • Success
    • Information
    • Verbose
  • -Source: (Optional) Specifies the source of the debug message.

Examples

Simple Example

Write-Debug "This is a debug message."

Specify Category

Write-Debug "This is a warning message." -Category Warning

Specify Source

Write-Debug "This message is from the Get-ChildItem cmdlet." -Source Get-ChildItem

Common Issues

  • Message not output: Ensure the -Message parameter is specified and contains a valid string.
  • Category not recognized: Use one of the valid categories listed in the Options/Flags section.

Integration

The Write-Debug cmdlet can be used together with other PowerShell commands to provide detailed tracing information. For example, the following script uses Write-Debug to output the results of each iteration of a loop:

1..10 | ForEach-Object {
    $i = $_
    Write-Debug "Iteration $i"
}
  • Trace-Command: Traces all commands in a script and outputs detailed information to the debug stream.
  • Get-Command: Retrieves information about specified commands, including their aliases, parameters, and scripts.