Geremachek2
Geremachek2

Reputation: 141

Can I change Read-Host so it doesn't always end in a colon?

Whenever I do a Read-Host the prompt always ends in :. Is there any way to change this? is it the -Prompt Flag?

Upvotes: 8

Views: 5497

Answers (2)

Mike C
Mike C

Reputation: 1

To semi-permanently rid PowerShell's Read-Host of its default colon, override the native Read-Host cmdlet with your own function of the same name as follows:

function Read-Host($prompt) {
  Write-Host "$prompt  " -NoNewline
  Microsoft.PowerShell.Utility\Read-Host }

Result:

Read-Host "What is your name?"
    
What's your name?  Mike
Mike

Either place that function at the top of your script or save it in a module and import it in each script as needed.

Upvotes: 0

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174835

As mentioned in the comments, there's no way to control how the executing host application presents the prompt when passing a Prompt message parameter argument.

What you can do instead, is call $Host.UI.ReadLine() directly from your script and prepend a message yourself:

Write-Host "No colons here>" -NoNewLine
$UserInput = $Host.UI.ReadLine()

Here's an example of what that looks like in powershell.exe:

Read-HostCustom function

Upvotes: 11

Related Questions