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

百般思念 提交于 2019-12-05 06:09:10

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
**********************
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!