3

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"}`

1 Answer 1

11

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.

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

Comments

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.