0

I wrote a script that can return you if a software is installed or not.

I would like to give it a bit of colors but I don't know how to concatenate 2 Write-Output on the same line as -NoNewline only works for Write-Host... which in this case I cannot use:

# The function we use to give color

function Positive {
    process { Write-Host $_ -ForegroundColor Green }
    }

function Negative {
    process { Write-Host $_ -ForegroundColor Red }
    }

# Is the software installed?
# '0' = NO, is not installed
# '1' = YES, is installed

$Check = '1'

function Check_Installation($Check){
    if ($Check -eq '0') {return $response =  "No, is not installed" | Negative}
    elseif ($Check -eq '1') {return $response =  "Yes, is installed" | Positive}
    }

$First_Phrase =  "Let's check if the software is installed: "

Write-Output "$First_Phrase", "$response"

Check_Installation($Check)

enter image description here

I know I can concatenate with

[string]::Concat("$First_Phrase", "$response")

but is not working.

4
  • 1
    If you just want to write something to the screen you should use Write-Host. Especially if you want to have it in another color. Commented Feb 25, 2019 at 0:34
  • Thank you @Olaf but this is not how it works if you use function as Write-Host will just write in the terminal without respecting the steps Commented Feb 25, 2019 at 0:43
  • But Write-Hostis the only way to have colored text. Commented Feb 25, 2019 at 0:45
  • If you search for powershell write-output colors on Google you will find many examples that shows how this is possible. But they all not apply to my situation because of the function. that's why I asked on StackOverflow Commented Feb 25, 2019 at 0:55

1 Answer 1

2

This works in console only as changing the foreground colour in ISE changes it for every line:

# The function we use to give color

function Set-Output {
    param ($colour, $str1, $str2)

    $t = $host.ui.RawUI.ForegroundColor
    $host.ui.RawUI.ForegroundColor = "$colour"

    $text = "$str1 $str2"

    Write-Output "$text"

    $host.ui.RawUI.ForegroundColor = $t

}

# Is the software installed?
# '0' = NO, is not installed
# '1' = YES, is installed

$Check = '1'

$First_Phrase =  "Let's check if the software is installed: "

Switch ($check) {
    ( 0 ) {Set-Output -colour RED -str1 $First_Phrase -str2 "No, is not installed" }
    ( 1 ) {Set-Output -colour GREEN -str1 $First_Phrase -str2  "Yes, is installed" }
}

Check_Installation($Check)

All that it is doing is concatenating the two strings and changing the foreground colour.

enter image description here

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

4 Comments

This looks very good. So there is no way to color just Yes, is installed and No, is not installed?
Unfortunately not that i know of, unless you have them on different lines the colour will be the same for each line. ( stackoverflow.com/questions/3896258/… ) will give you an idea of how Write-Output works. If you must have different colours on the same line you have to use Write-Host with the -nonewline parameter
What is the reason you cannot use write-host by the way?
This is the reason that forces me to use Write-Output stackoverflow.com/questions/54344622/…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.