Redirect Write-Host statements to a file

前端 未结 12 1753
鱼传尺愫
鱼传尺愫 2020-11-29 08:39

I have a PowerShell script that I am debugging and would like to redirect all Write-Host statements to a file. Is there an easy way to do that?

12条回答
  •  离开以前
    2020-11-29 08:48

    Until PowerShell 4.0, Write-Host sends the objects to the host. It does not return any objects.

    Beginning with PowerShell 5.0 and newer, Write-Host is a wrapper for Write-Information, which allows to output to the information stream and redirect it with 6>> file_name.

    http://technet.microsoft.com/en-us/library/hh849877.aspx

    However, if you have a lot of Write-Host statements, replace them all with Write-Log, which lets you decide whether output to console, file or event log, or all three.

    Check also:

    • Add-Content
    • redirection operators like >, >>, 2>, 2>, 2>&1
    • Write-Log
    • Tee-Object
    • Start-Transcript.

提交回复
热议问题