locked
using color with Read-Host RRS feed

  • Question

  • Is there a way to use -foregroundcolor with Read-Host?

    In the below example, I would like to change the color of "[quit]", but leave all other text at the default color:

    $input = Read-Host "Select an option or type [quit] to exit"

    Tuesday, April 3, 2012 2:28 PM

Answers

  • Write-Host "Select an option or type" -NoNewline
    Write-Host " [quit] " -ForegroundColor Yellow -NoNewline
    Write-Host "to exit: " -NoNewline
    $input = Read-Host

    • Proposed as answer by Bigteddy Tuesday, April 3, 2012 2:48 PM
    • Marked as answer by tickermcse76 Tuesday, April 3, 2012 3:21 PM
    Tuesday, April 3, 2012 2:46 PM

All replies

  • Easy one:  No.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    • Proposed as answer by Kazun Tuesday, April 3, 2012 2:47 PM
    Tuesday, April 3, 2012 2:36 PM
  • Write-Host "Select an option or type" -NoNewline
    Write-Host " [quit] " -ForegroundColor Yellow -NoNewline
    Write-Host "to exit: " -NoNewline
    $input = Read-Host

    • Proposed as answer by Bigteddy Tuesday, April 3, 2012 2:48 PM
    • Marked as answer by tickermcse76 Tuesday, April 3, 2012 3:21 PM
    Tuesday, April 3, 2012 2:46 PM
  • Works great; thanks.
    Tuesday, April 3, 2012 3:21 PM
  • There is no easy way, but following copy/paste function should help.

    function take_input() {
        param
        (
            [Parameter(Position = 0, ValueFromPipeline = $true)]
            [string]$msg,
            [string]$BackgroundColor = "Black",
            [string]$ForegroundColor = "DarkGreen"
        )
    
        Write-Host -ForegroundColor $ForegroundColor -NoNewline $msg;
        return Read-Host
    }
    
    $choice = take_input 'Perform setup? [Y/N]'




    Sunday, November 17, 2019 12:40 PM