5

when I use this command pwsh -c echo hello in cmd I get the following output:

C:\>pwsh -c echo hello
hello

C:\>

I do not get that line break at the end when I run it on powershell:

PS C:\> pwsh -c echo hello
hello
PS C:\>

So I think the problem is in cmd. I know this is not such a problem and have an easy fix but I have some programs uses cmd to access powershell and removing that line break is not that fun. So is there any fix to prevent cmd to add that line ?

2
  • 3
    The Windows Command Processor cmd.exe always outputs an empty line after execution of a command finished before displaying the prompt for next user input. Windows PowerShell powershell.exe does not do that. So the empty line is not produced by execution of pwsh -c echo hello or just echo hello, but by cmd.exe itself before printing the prompt C:\> in your case. So there is nothing to change. You can see the behavior by running in a command prompt window: echo Hello Yusef!& echo Is your question answered? There is no empty line between the two output lines, just above the prompt. Commented Mar 6, 2022 at 14:10
  • 2
    There is on Super User the question How to stop the Windows command line from outputting an empty line after each command? There could be used echo Hello Yusef!& set /P =Is your question answered?<nul to get the two lines output without an empty line because of the prompt text Is your question answered? is output without newline character(s) and so the newline character(s) output by cmd.exe before the prompt terminates the line with the output prompt text. Commented Mar 6, 2022 at 14:44

1 Answer 1

4

Mofi has provided the crucial pointers in comments:

  • When executing a command interactively, cmd.exe unconditionally appends a a newline (line break) to the command's output, presumably for readability and perhaps also to ensure that the next prompt always starts on a new line.

    • This applies irrespective of what that command is. In other words: It doesn't matter that your command happens to be a PowerShell command.
  • However, that trailing newline does not become part of the command's output, therefore programmatic processing of a command's output is not affected, such as when you redirect > to a file or process the output lines one by one with for /f.

    • In other words: for programmatic processing you need not remove the trailing newline, because it isn't part of the actual command output.

    • Conversely, if you really need to in effect suppress the trailing newline for display, you'll have to modify the command's output - if that is even an option - so that the output itself doesn't end in a newline, as shown in this SuperUser answer for cmd.exe's own echo command; for PowerShell, you could do pwsh -c Write-Host -NoNewLine hello.


Edge case:

When capturing output from a batch file that is running without @echo off (or with echo on) - in which case the trailing newlines do become part of the output - you can filter out empty lines by piping to findstr /r /v /c:"^$" (as also shown in the linked answer); e.g.

foo.cmd | findstr /r /v /c:"^$"

However, note that all empty lines are filtered out this way - potentially including actual empty lines in the output from commands executed by the batch file.

If preventing that is required, a more sophisticated approach is required, which, however (a) relies on the standard prompt string (e.g., C:\>) being used and (b) can still yield false positives:

foo.cmd | powershell -nop -c "@($Input) -join \"`n\" -replace '\n(?=[a-z]:\\.*?>)'"

Finally note that if you execute the above commands without capturing or redirecting their output, their overall output in the cmd.exe console will again have a trailing newline.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.