How to perform short-circuit evaluation in Windows PowerShell 4.0? How to perform short-circuit evaluation in Windows PowerShell 4.0? powershell powershell

How to perform short-circuit evaluation in Windows PowerShell 4.0?


A simple set of test cases show that short-circuiting works:

PS C:\> 1 -eq 0 -or $(Write-Host 'foo')fooFalsePS C:\> 1 -eq 1 -or $(Write-Host 'foo')TruePS C:\> 1 -eq 1 -and $(Write-Host 'foo')fooFalsePS C:\> 1 -eq 0 -and $(Write-Host 'foo')False


I was looking for the same, came across this answer. The best so far in my opinion...

$cueNumber = 512@{ $true = "This is true"; $false = "This is false" }[$cueNumber -gt 500]

Reference: https://adamtheautomator.com/a-simpler-ifthen-conditional-logic-in-powershell/