2

I want to collapse these Powershell commands:

$colour = ""
$colour1 = "Red"
$colour2 = "Green"
if ($var -eq 0) {
    # failure
    $colour = $colour1
} Else {
    # success
    $colour = $colour2
}
Write-Host ("Answer = " + $var) -ForegroundColor $colour

into one line, using the output of the if expression above as the input argument to Write-Host, something like:

Write-Host ("Answer = " + $var) -ForegroundColor &{if ($var -eq 0) {'Red'} else {'Green'}

Things I have tried and which failed:

Write-Host ("Answer = " + $var) -ForegroundColor &{if ($var -eq 0) {'Red'} else {'Green'}
Write-Host ("Answer = " + $var) -ForegroundColor `&{if ($var -eq 0) {'Red'} else {'Green'}`
Write-Host ("Answer = " + $var) -ForegroundColor `{if ($var -eq 0) {'Red'} else {'Green'}`
Write-Host ("Answer = " + $var) -ForegroundColor &`{if ($var -eq 0) {'Red'} else {'Green'}`
Write-Host ("Answer = " + $var) -ForegroundColor (&`{if ($var -eq 0) {'Red'} else {'Green'}`)
Write-Host ("Answer = " + $var) -ForegroundColor `{if ($var -eq 0) {return 'Red'} else {return 'Green'}`
Write-Host ("Answer = " + $var) -ForegroundColor `&{if ($var -eq 0) {return 'Red'} else {return 'Green'}`
Write-Host ("Answer = " + $var) -ForegroundColor `&{if ($var -eq 0) {return "Red"} else {return "Green"}`
Write-Host ("Answer = " + $var) -ForegroundColor &{if ($var -eq 0) {return "Red"} else {return "Green"}
Write-Host ("Answer = " + $var) -ForegroundColor &{if ($var -eq 0) {return "Red"} else {return "Green"}}
Write-Host ("Answer = " + $var) -ForegroundColor `&{if ($var -eq 0) {return "Red"} else {return "Green"}}`
Write-Host ("Answer = " + $var) -ForegroundColor &`{if ($var -eq 0) {return "Red"} else {return "Green"}}`
Write-Host ("Answer = " + $var) -ForegroundColor `{if ($var -eq 0) {return "Red"} else {return "Green"}}`
Write-Host ("Answer = " + $var) -ForegroundColor `if ($var -eq 0) {return "Red"} else {return "Green"}`
skeetastax
  • 1,016
  • 8
  • 18

1 Answers1

7

To pass output from entire statements (such as if) as command arguments, enclose them in $(...), the subexpression operator.

A simplified example:

# Note the use of $(...) around the `if` statement.
PS> $var = 0; Write-Host $(if ($var -eq 0) { 'zero' } else { 'nonzero' })
zero

Note that if only an expression's or command's output is needed, use of (...), the grouping operator is sufficient (and side-effect-free).

E.g., the PowerShell (Core) 7+ alternative to an if statement is the ternary operator, whose use is an expression:

# PowerShell (Core) 7+ only.
# Since an expression is used, (...) is sufficient.
PS> $var = 0; Write-Host ($var -eq 0 ? 'zero' : 'nonzero')
zero

For commands (pipelines), (...) is sufficient too.

# Since a command is used, (...) is sufficient.
Write-Output (Get-Item *.txt | Where-Object Length -ge 1kb)

For more information on $(...) vs. (...), see this answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 2
    @skeetastax, that's because you mistyped `-ForegroundColor` (missing `e`), so the parameter _name_ wasn't recognized and `-ForgroundColor` was therefore treated as just another _positional_ argument to print. In other words: your problem is incidental, and this answer still applies. – mklement0 Jun 29 '21 at 12:33