is it possible to do something like below in powershell?
$condition = "-eq 10"
$a=11
if ($a &$condition) {"yes"} else {"no"}
basically to create part of the statement outside the statement and then use it dynamically?
I am aware I can create the whole statement in variable and then run it using Invoke-Expression or as &$cmd, but I have a very long and complex if/else statement and trying to build part of it on the fly.
Hope it makes sense 🙂
Yes, if you’re OK with swapping order e.g.:
You can do prefix style notation using scriptblocks
{code}with PowerShell but not infix nor postfix. That’s because PowerShell wants the arguments to follow the scriptblock (not before and not between).