3

I want to print a Warning in PowerShell as a prompt and then read the answer on the same line. The problem is that Write-Warning prints a newline at the endof the message, and one alternative, Read-Host -Prompt, doesn't print the prompt to the warning stream (or print in yellow). I've seen Write-Warning -WarningAction Inquire, but I think that's a little verbose and offers options I don't want.

The best I've done is:

  $warningMsg= "Something is wrong. Do you want to continue anyway Y/N? [Y]:"
  Write-Host -ForegroundColor yellow -NoNewline $warningMsg
  $cont = Read-Host

This works great--prints the yellow prompt and then reads the input on the same line--but I'm wondering about the warnings I've seen against using Write-Host, if it's more appropriate to figure out some way to print to the warning stream without a newline. Is there a way to do that? I've noticed that Write-Host seems to be a wrapper to write to the Info stream, but I don't see any way to write to warnings without a new line ([Console]::Warning.WriteLine() doesn't exist for example).

xdhmoore
  • 8,935
  • 11
  • 47
  • 90
  • the warnings about `Write-Host` are ... often overblown. [*grin*] if you want to write to the screen AND you want color AND you are willing to give up the ability to cleanly disable that writing ... then `Write-Host` is the correct thing to use. – Lee_Dailey Jul 10 '20 at 00:36
  • 1
    Write-Host "evilness" disappeared with the introduction of PS 5. I understand that before that it was problematic but nowadays, it does write in the informaiton stream (see answer for more details about that) @Lee_Dailey I was curious about the "give up" fact so I checked. You can still disable that writing through `-InformationAction` parameter (see the Bonus section in my answer [grin back]) – Sage Pourpre Jul 10 '20 at 01:45
  • @SagePourpre - ah! thanks [*grin*] ... i had forgotten about how `-InformationAction` changes `Write-Host` ... [*blush*] – Lee_Dailey Jul 10 '20 at 12:43

1 Answers1

4

You can't do what you want with Write-Warning.

Therefore, I will answer regarding your other concern. Write-Host is perfectly fine to use in a PowerShell 5+ script.

If you look at the articles recommending against its use, you will notice that the vast majority (if not all) were written before the introduction of PowerShell 5.

Nowadays, Write-Host is a wrapper around Write-Information.

The official documentation confirms this:

Starting in Windows PowerShell 5.0, Write-Host is a wrapper for Write-Information This allows you to use Write-Host to emit output to the information stream. This enables the capture or suppression of data written using Write-Host while preserving backwards compatibility.

The $InformationPreference preference variable and -InformationAction common parameter do not affect Write-Host messages. The exception to this rule is
-InformationAction Ignore, which effectively suppresses Write-Host output.

Writing to the information stream using Write-Host and / or Write-information won't create problems with your output string.

Stream #    Description          Introduced in
1           Success Stream       PowerShell 2.0
2           Error Stream         PowerShell 2.0
3           Warning Stream       PowerShell 3.0
4           Verbose Stream       PowerShell 3.0
5           Debug Stream         PowerShell 3.0
6           Information Stream   PowerShell 5.0
*           All Streams          PowerShell 3.0

Bonus

You can also control the visibility of the information stream if you use an advanced function through the -InformationAction parameter, provided you also bind the given parameter value to the Write-Host statements in the function.

For instance, if you wanted to disable the Information stream by default unless requested otherwise:

function Get-Stuff {
     [CmdletBinding()]
     param ()

     if (!$PSBoundParameters.ContainsKey('InformationAction')) {
        $InformationPreference = 'Ignore'
     }
     Write-Host 'This is the stuff'  -InformationAction $InformationPreference -ForegroundColor Green
}

# Hidden by default
Get-Stuff
# Force it to show
Get-Stuff -InformationAction Continue

Note

While technically it is not possible to use Write-Warning -NoNewLine, you could look into manipulating the cursor position and resetting it to the end of the previous line, therefore doing the same.

However, I have limited experience with that and my observations regarding the subject were that you might end up having to create exceptions to comply with limitations of some console environments. In my opinion, this is a bit overkill...

Additional references About_redirections

mklement0
  • 382,024
  • 64
  • 607
  • 775
Sage Pourpre
  • 9,932
  • 3
  • 27
  • 39
  • It seems if I could just redirect Info to Warning, maybe I could do it, but that doesn't see to be possible yet. – xdhmoore Jul 10 '20 at 02:02