Freigeben über


PS without BS: Replacing Select Case True

This is a short blog to describe how to replace VBScript's brilliant "Select Case True" with PowerShell.

Instead of evaluating one variable, what happens when you need to evaluate multiple variables and make sure that one of them returns true? Instead of multiple "if" statements, VBScript used Select Case True. PowerShell can do the same.

Here is a quick code snippet:

 
$myVar="abc"
switch ($true){
($myOtherVar -eq "123") {write-host "MyOtherVar is 123"}
($MyVar -eq "abc") {write-host "MyVar is abc"}
default {write-host "None of the above"}
}

In this case, you can see I am looking at 2 variables. Whichever comes back true, the action will be taken. If none are, default will kick and say "None of the above".

Happy Scripting!

— Easy link to my blog: https://aka.ms/leesteve
If you like my blogs, please share it on social media and/or leave a comment.