PS -nonewline not work on my console

Pio MOON 21 Reputation points
2020-09-10T08:00:07.783+00:00

Hello from PS very beginner.

https://video2.skills-academy.com/en-us/powershell/module/microsoft.powershell.utility/write-host?view=powershell-7

when I try cmdlet "write-host -nonewline" on my PS console as tutorial(windows10 Home, PS 5.1)

PS C:\Windows\system32> write-host "no newline test " -NoNewline
no newline test
PS C:\Windows\system32>


PS C:\Windows\system32> Write-Host "second string"
second string
PS C:\Windows\system32>

I think it should work as this;
PS C:\Windows\system32> write-host "no newline test " -NoNewline

> > >Write-Host "second string"

no newline test second string


PS C:\Windows\system32>

But the second example(-separator) is just working fine.

what's wrong with this?

Help highly appreciated.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,551 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue 37,621 Reputation points Microsoft Vendor
    2020-09-10T09:54:19.93+00:00

    Hi,
    Don't run the two commands separately in the terminal. The example should run as a powershell script. Please create a script in Windows Powershell ISE and press F5 to run it.

    The Windows Powershell ISE
    https://video2.skills-academy.com/en-us/powershell/scripting/windows-powershell/ise/introducing-the-windows-powershell-ise?view=powershell-7

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 46,811 Reputation points
    2020-09-10T18:33:13.4+00:00

    The intervening prompt between the two Write-Hosts is the problem. Put both of them on the same line and it works, I think, in the way you expect.

    PS C:\> write-host "no newline test " -NoNewline; Write-Host "second string"
    no newline test second string
    PS C:\>
    

    Running them on separate lines in a script (where there's no intervening prompt) works too, as IanXue-MSFT states.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.