I was writing some long, complicated Powershell scripts recently, and complicated scripts call for plenty of tracing/debug logging. These scripts were to be run as scheduled tasks, and it was important that I saved this debug logging information to a file, so I figured that instead of re-inventing the wheel, I'd just use Start-Transcript in conjunction with Write-* cmdlets to automatically capture all the output.
This almost worked fine, except that the lines in the resulting transcript file were word-wrapped at 80 characters. This made the transcripts ugly and hard to read. The only thing worse than reading debug logs while trying to diagnose a subtle bug is having to do it when the debug logs are ugly and hard to read.
Hm, would it help if I resized the Powershell host window and buffer at the beginning of the script, like so?
$Host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size 180, 3000
$Host.UI.RawUI.WindowSize = New-Object System.Management.Automation.Host.Size 180, 50
No, that did not help.
As it turns out, the Write-Host cmdlet does not exhibit the same word-wrapping limitation as its Write-Debug and Write-Warning brethren. So instead of changing the 900 Write-Debug cmdlets already in our script, let's just "override" the Write-Debug cmdlet:
Function Global:Write-Debug([Parameter(Mandatory=$True, ValueFromPipeline=$True)][String]$Message)
If ($DebugPreference -NE 'SilentlyContinue')
Write-Host "DEBUG: $Message" -ForegroundColor 'Yellow'
Ah, no more needless word wrapping in my transcripts now. Just be careful about putting stuff in the Global scope if you're working with other actors in the same environment.