PowerShell Script Logging and Debugging: A Comprehensive Guide

If you’re working on a PowerShell script and want to log all activities or debug it effectively, here’s a detailed guide to help you. This post will cover how to enable verbose output, handle errors, and implement global settings to streamline your debugging process.


Enable Verbose Output

PowerShell provides a built-in verbose stream for detailed logging.

Using -Verbose

Add the -Verbose flag to commands for detailed logs:

Get-Process -Verbose

Enable Verbose Globally

Set $VerbosePreference at the top of your script:

$VerbosePreference = "Continue"

This enables verbose logging for all commands supporting -Verbose without needing to add the flag repeatedly.


Handle Errors Effectively

PowerShell scripts handle errors based on $ErrorActionPreference.

Enable Strict Error Handling

Set $ErrorActionPreference to stop the script on errors:

$ErrorActionPreference = "Stop"

Combine -ErrorAction and -Verbose

Use both options for granular control:

Get-Item "C:\NonExistentFile.txt" -ErrorAction Stop -Verbose

Debugging with $DebugPreference

Enable debugging messages for more in-depth information:

$DebugPreference = "Continue"
Write-Debug "This is a debug message."

Trace Logging with Set-PSDebug

Use Set-PSDebug to trace script execution:

Set-PSDebug -Trace 2
  • Trace 1: Basic information (functions, scripts, external commands).
  • Trace 2: Detailed variable assignments and command execution.

Disable tracing when done:

Set-PSDebug -Off

Use Try-Catch for Error Tracking

Wrap commands in a Try-Catch block for better error handling:

Try {
Get-Item "C:\NonExistentFile.txt" -Verbose
} Catch {
Write-Host "An error occurred: $($_.Exception.Message)" -ForegroundColor Red
}

Redirect Logs to a File

Log all output, including verbose, error, and debug messages:

$LogFile = "C:\Logs\CompleteLog.txt"
{
Get-Process -Verbose
Get-Item "C:\NonExistentFile.txt" -ErrorAction Stop
} *>> $LogFile

Global Verbose Setting for Functions

To enable verbose logging in functions:

$VerbosePreference = "Continue"

function Get-CustomInfo {
Write-Verbose "Fetching custom information..."
Write-Output "Custom Information"
}

Get-CustomInfo

Combine Verbose and Debug

Enable both for comprehensive logging:

$VerbosePreference = "Continue"
$DebugPreference = "Continue"

Write-Output "Starting script..."
Write-Debug "Debugging: About to list processes."
Get-Process

Scoped Verbose Logging

Apply verbose logging to specific sections using a script block:

& {
$VerbosePreference = "Continue"
Write-Verbose "Verbose logging for this block."
Get-Process
}

Start Logging to a File with Start-Transcript

For full session logging:

Start-Transcript -Path "C:\Logs\VerboseLog.txt" -Append

Write-Verbose "Script starting..."
Get-Process
Write-Verbose "Script ending..."

Stop-Transcript

By implementing these techniques, you can effectively log all activities and debug your PowerShell scripts with ease. Whether you’re handling errors, enabling verbose output, or redirecting logs, these strategies will help streamline your scripting process.

Junaid Ahmed
Junaid Ahmed

Junaid Ahmed is a Cloud Infrastructure and Identity Management expert with 10+ years of experience specializing in Azure Entra ID, ADFS, Hybrid Identity, and Azure Infrastructure Management. He has a proven track record of leading secure identity solutions, deploying high-value security projects, and troubleshooting complex Azure issues for global clients. Junaid excels in enhancing system performance, facilitating seamless collaboration across organizations, and delivering expert guidance on cloud migrations and infrastructure optimization. He seeks to leverage his expertise in a challenging Cloud Solution Architect role to drive success through innovative cloud solutions.

Articles: 30

Leave a Reply

Your email address will not be published. Required fields are marked *

WordPress Appliance - Powered by TurnKey Linux