Tales from the code review…
How do you test for a switch type in a PowerShell script?
How do you test for the *absence* of a switch in a PowerShell Script?
Came across this recently, and decided to dig into it further.
IN the example above, We have a function with a single switch argument. We then test against that argument, displaying “do something” if it’s set, and “Don’t do it” if not set.
PS C:\Users\Keith> C:\Users\Keith\Source\Example\test-switches.ps1 Don't do it Don't do it Do Something Do Something
Cool! Um… where did the “Never going to do it!” go? Well turns out that even when we don’t specify -test as an argument to the function, it’s still a switch defined as IsPresent = $false. So testing to see if it’s equal to $null will always fail, because it’s never $null.