Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

How to Capture PowerShell Trace and Debug Messages in DebugView

When you’re running PowerShell scripts as scheduled tasks, background jobs, Runspace jobs, or on remote servers, normal Write-Host or Write-Debug output often disappears. That’s where System.Diagnostics.Trace comes in.

System.Diagnostics.Trace is a built-in.NET class designed to instrument, monitor, and log application execution at runtime. Developers use it to embed diagnostic messages throughout code to track program flow, measure performance, and catch errors — without touching standard PowerShell output streams.

Why not just use Write-Debug?

Write-Debug only shows up when $DebugPreference = "Continue" and it’s tied to the host. That’s fine for interactive scripts, but not for automation.

[System.Diagnostics.Trace] and [System.Diagnostics.Debug] broadcast diagnostic messages globally at the.NET level. This means your messages can be picked up by external tools like Sysinternals DebugView, even if the script is running headless, multithreaded, or on a remote machine. You get logging without polluting Success, Error, Warning, or Verbose streams.

Setting up DebugView to capture PowerShell traces

  1. Download DebugView
    Get it from Microsoft Sysinternals: https://learn.microsoft.com/en-us/sysinternals/downloads/debugview/ or directly as dbgview.zip. Extract and launch DebugView.exe.

  2. Connect to local machine
    If DebugView doesn’t auto-connect, go to Computer tab > Connect Local.

  3. Enable Capture Win32
    In DebugView, under Options, make sure Capture Win32 is checked. This lets it catch.NET framework diagnostics coming from PowerShell.

Download DebugView (Dbgview) application from https://learn.microsoft.com/en-us/sysinternals/downloads/debugview/ or dbgview.zip. Lunch DebugView.exe. If local computer machine is not loaded, open Computer tab and select Connect Local. For remote server click connect.

DebugView interface showing how to connect locally on computer using the Computer menu for Powershell trace log for dbg

Open PowerShell on and execute below commands. This allows "Capture Win32" enabled in the options menu to catch these specific .NET framework diagnostics from a PowerShell session.

[System.Diagnostics.Debug]::Write("test")
[System.Diagnostics.Trace]::WriteLine('Test Message')
[System.Diagnostics.Trace]::TraceError('This is Error')

You’ll immediately see the output appear in DebugView.

  • Write vs WriteLine: WriteLine appends a newline.
  • TraceError, TraceWarning, TraceInformation: Let you categorize severity.

When this is extremely helpful

  1. Remote execution: When a script runs via Invoke-Command or PSRemoting and you can’t see the console.
  2. Scheduled Tasks: Task Scheduler hides output. DebugView on the server will still capture traces.
  3. Runspace / Multithreading: Multiple threads writing to host streams gets messy. Trace messages are centralized and timestamped in DebugView.

This approach makes PowerShell logging simple and decoupled. You instrument once, and any support engineer can attach DebugView to watch live diagnostics without modifying your script or redirecting streams.

PowerShell DebugView output showing System.Diagnostics.Trace WriteLine and TraceError messages on local machine local debugging session

Pro tip: Add a switch to your scripts to enable trace logging only when needed:

$EnableTrace =  $true
if ($EnableTrace) {
    [System.Diagnostics.Trace]::Listeners.Add(
        [System.Diagnostics.DefaultTraceListener]::new()
    )
    [System.Diagnostics.Trace]::AutoFlush = $true
    [System.Diagnostics.Trace]::WriteLine("Script started at $(Get-Date)")
}

That way you get powerful, global, non-intrusive logging exactly when troubleshooting demands it.

Useful Articles
POWERSHELL: INSTALLING AND CONFIGURING ACTIVE DIRECTORY 
POWERSHELL ACTIVE DIRECTORY: ADD OR UPDATE (CHANGE) MANAGER NAME IN ORGANIZATION TAB OF USER
POWERSHELL ACTIVE DIRECTORY: ADD OR UPDATE PROXYADDRESSES IN USER PROPERTIES ATTRIBUTE EDITOR
Add multiple proxy addresses with Microsoft PowerShell in Active Directory Groups
Creating a password reset tool with PowerShell GUI
Microsoft Active directory additional features - AD Recycle Bin Powershell
Powershell: Temporary group membership on Windows 2016 Active Directory PAM (Privileged Access Management Feature)
Adding user to administrators from another cross domain - Part 1
PowerShell: Copy group membership from one user to another user in Active Directory
PowerShell GUI: Copy group membership from one user to another user in Active Directory
PowerShell Active Directory: Sync group membership from one user to another user and move to OU

 

Go Back

Comment

Protected by Mathcha

Blog Search

Page Views

1 5 1 4 6 0 3 2

Archive

Follow me on Blogarama