BuddyJoe
BuddyJoe

Reputation: 71141

PowerShell - Uppercase after a "Ternary" If Statement

What is the correct way to do this in PowerShell?

$action = if ($args.Length > 0) { $args[0] } else { Read-Host 'Action' } #.ToUpper()
echo $action

The following seems like a code smell

$action = if ($args.Length > 0) { $args[0] } else { Read-Host 'Action' }
$action = $action.ToUpper()
echo $action

Upvotes: 1

Views: 484

Answers (1)

briantist
briantist

Reputation: 47842

The first code block you have will almost work as written (you can assign the result of an if/else statement).

$action = $(if ($args.Length -gt 0) { $args[0] } else { Read-Host 'Action' }).ToUpper()

You just need to use the -gt (greater than) operator instead of >, and wrap it in parentheses.

Upvotes: 4

Related Questions