6

I'd like to Write-Verbose a lot of data to an out file. Here's how I'm doing it.

Start-Transcript -Path $TargetDir\RunUnitTests.log -Width 1000000
Write-Verbose "five million character lines and stuff"

This works great, except that the output is auto wrapped to the standard width of a console, this makes the log look absolutely terrible.

I found a solution heredead link removed , but it's so involved and complicated that I don't want to just throw this in my script below a comment #Thar be dragons.

Is there a better way to do this?

1
  • For the related problem of prevent wrapping in all verbose output, not just from explicit calls to Write-Verbose, see this question. Commented Jan 8, 2017 at 13:02

1 Answer 1

5

Here is a very simple workaround based on Write-Host which does not have such a problem. In the beginning of the session install/dot-source the replacement of the default Write-Verbose:

function global:Write-Verbose
(
    [string]
    $Message
)
{
    # check $VerbosePreference variable
    if ($VerbosePreference -ne 'SilentlyContinue') {
        # do this via Write-Host
        Write-Host "VERBOSE: $Message" -ForegroundColor 'Yellow'
    }
}

Then this works as needed:

$VerbosePreference = 'Continue'
Start-Transcript -Path .\RunUnitTests.log
Write-Verbose ("verbose writes five million character lines and stuff. " * 20)

That is: it takes into account $VerbosePreference, it writes to host in yellow, transcript output is not wrapped and it is still marked VERBOSE.

**********************
Windows PowerShell Transcript Start
Start time: 20101105055855
**********************
Transcript started, output file is .\RunUnitTests.log
VERBOSE: verbose writes ... <long line text> ... and stuff.
**********************
Windows PowerShell Transcript End
End time: 20101105055855
**********************
Sign up to request clarification or add additional context in comments.

1 Comment

You might perhaps want to also decorate the parameter to support pipelining [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.