How do I output text without a newline in PowerShell?

前端 未结 17 2235
独厮守ぢ
独厮守ぢ 2020-12-07 15:13

I want my PowerShell script to print something like this:

Enabling feature XYZ......Done

The script looks something like this:



        
相关标签:
17条回答
  • 2020-12-07 15:58

    The answer by shufler is correct. Stated another way: Instead of passing the values to Write-Output using the ARRAY FORM,

    Write-Output "Parameters are:" $Year $Month $Day
    

    or the equivalent by multiple calls to Write-Output,

    Write-Output "Parameters are:"
    Write-Output $Year
    Write-Output $Month
    Write-Output $Day
    Write-Output "Done."
    

    concatenate your components into a STRING VARIABLE first:

    $msg="Parameters are: $Year $Month $Day"
    Write-Output $msg
    

    This will prevent the intermediate CRLFs caused by calling Write-Output multiple times (or ARRAY FORM), but of course will not suppress the final CRLF of the Write-Output commandlet. For that, you will have to write your own commandlet, use one of the other convoluted workarounds listed here, or wait until Microsoft decides to support the -NoNewline option for Write-Output.

    Your desire to provide a textual progress meter to the console (i.e. "....") as opposed to writing to a log file, should also be satisfied by using Write-Host. You can accomplish both by collecting the msg text into a variable for writing to the log AND using Write-Host to provide progress to the console. This functionality can be combined into your own commandlet for greatest code reuse.

    0 讨论(0)
  • 2020-12-07 16:01

    A simplification to FrinkTheBrave's response:

    [System.IO.File]::WriteAllText("c:\temp\myFile.txt", $myContent)
    
    0 讨论(0)
  • 2020-12-07 16:02

    I cheated, but I believe this is the only answer that addresses every requirement. Namely, this avoids the trailing CRLF, provides a place for the other operation to complete in the meantime, and properly redirects to stdout as necessary.

    $c_sharp_source = @"
    using System;
    namespace StackOverflow
    {
       public class ConsoleOut
       {
          public static void Main(string[] args)
          {
             Console.Write(args[0]);
          }
       }
    }
    "@
    $compiler_parameters = New-Object System.CodeDom.Compiler.CompilerParameters
    $compiler_parameters.GenerateExecutable = $true
    $compiler_parameters.OutputAssembly = "consoleout.exe"
    Add-Type -TypeDefinition $c_sharp_source -Language CSharp -CompilerParameters $compiler_parameters
    
    .\consoleout.exe "Enabling feature XYZ......."
    Write-Output 'Done.'
    
    0 讨论(0)
  • 2020-12-07 16:06

    You simply cannot get PowerShell to omit those pesky newlines... There is no script or cmdlet that does. Of course, Write-Host is absolute nonsense, because you can't redirect/pipe from it!

    Nevertheless, you can write your own EXE file to do it which is what I explained how to do in Stack Overflow question How to output something in PowerShell.

    0 讨论(0)
  • 2020-12-07 16:06

    It may not be terribly elegant, but it does exactly what OP requested. Note that the ISE messes with StdOut, so there will be no output. In order to see this script work it can't be run within the ISE.

    $stdout=[System.Console]::OpenStandardOutput()
    $strOutput="Enabling feature XYZ... "
    $stdout.Write(([System.Text.Encoding]::ASCII.GetBytes($strOutput)),0,$strOutput.Length)
    Enable-SPFeature...
    $strOutput="Done"
    $stdout.Write(([System.Text.Encoding]::ASCII.GetBytes($strOutput)),0,$strOutput.Length)
    $stdout.Close()
    
    0 讨论(0)
提交回复
热议问题