Avoiding newline in write-output

前端 未结 4 953
故里飘歌
故里飘歌 2020-12-03 17:23

I want to collect all output from my script in a log file and must use write-output instaed of write-host.

Write-Output \"Server:\" $a looks like

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-03 17:40

    There's good information in the existing answers, but let me attempt a pragmatic summary:

    • When printing to the console or redirecting to a file, Write-Output separates multiple arguments by a newline each.

    • Therefore, in order to produce single-line output, you must "pre-assemble" the parts to output into a single string that you pass as a single argument.

      • Aaron Jensen's helpful answer shows you 3 alternative ways of doing that.
    • Except to suppress enumeration of collections, you generally do not need to use Write-Output explicitly, because PowerShell by default sends anything that is not captured in a variable or redirected elsewhere or sent through the pipeline to the [success] output stream (which Write-Output also writes to); thus, in the case at hand, the following is sufficient:

    "Server: $a"  # what this expandable string expands to is *implicitly* output
    

    What may be confusing is that the Write-Host cmdlet acts differently, but it's important to note that it has a different purpose.

    # IMPORTANT: Do not use Write-Host to output *data*; only use it to write
    #            directly to the host (console).
    PS> Write-Host "Server:"     $a    # multiple spaces used on purpose
    Server: srv1
    

    Write-Host, unlike Write-Output, separates multiple arguments by a single space each.
    There are other important differences, which are summarized in this answer of mine.


    Given the generic title of the question, let's also address how to suppress a trailing newline:

    • For printing to the console only, you can use Write-Host -NoNewline.

    • With data output, be it via Write-Output, implicit output, or from another command:

      • You cannot prevent a trailing newline when the output is sent to the console.

      • Unfortunately, as of Windows PowerShell v5.1 / PowerShell Core v6.0.0, you cannot prevent a trailing newline when sending text to external programs via the pipeline - see this GitHub issue.

      • In PSv5+, however, you can prevent a trailing newline when capturing output in a file, by using Out-File -NoNewline or Set-Content -NoNewline (in PSv4-, you must use the .NET Framework directly); by contrast, redirecting to a file with > does append a trailing newline.

        • Caveat: If you're outputting multiple objects, -NoNewline not only suppresses a trailing newline, but also newlines between these objects.
        • For the differences between Out-File and Set-Content and when to choose which, see this answer of mine.

提交回复
热议问题