...Because someone's gotta tell the story...


To return to the main Blog List, click Full Blog Listing.


Site Updates

PS without BS: Replacing Select Case True

Saturday, April 28, 2018 in Powershell (Views: 2322)
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:
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!


Related Blogs You May Be Interested In:

To leave a comment, please log in and/or register.