Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write-Verbose output that doesn't wrap to command width in Powershell

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?

like image 936
user467384 Avatar asked Nov 05 '10 03:11

user467384


People also ask

How do I get the verbose output in PowerShell?

By default, the verbose message stream is not displayed, but you can display it by changing the value of the $VerbosePreference variable or using the Verbose common parameter in any command. Also, Write-Verbose writes to the verbose output stream and you can capture it separately.

What does Write verbose do in PowerShell?

The Write-Verbose cmdlet writes text to the verbose message stream in PowerShell. Typically, the verbose message stream is used to deliver more in depth information about command processing.

What does verbose parameter do?

The -verbose parameter tells you what has been done. If you are doing something risky, then the verbose parameter doesn't provide protection against ill-advised actions like the -whatif or =confirm parameters, at least if you haven't worked out the precise effect of the command.

What is the difference between Write-host and Write-output in PowerShell?

In a nutshell, Write-Host writes to the console itself. Think of it as a MsgBox in VBScript. Write-Output , on the other hand, writes to the pipeline, so the next command can accept it as its input. You are not required to use Write-Output in order to write objects, as Write-Output is implicitly called for you.


1 Answers

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
**********************
like image 117
Roman Kuzmin Avatar answered Oct 28 '22 11:10

Roman Kuzmin