if, oh if, wherefore art thou, if?

posh-getrand-v2

Recently, we looked at using PowerShell for generating pseudo-random names. The example script provided in that article was intentionally PowerShell version 1.0 and PowerShell version 2.0 compatible.

Those of you who know that PowerShell 2.0 is included in Windows 7 and Windows Server 2008 R2 and the foundation for the Exchange Management Shell in Exchange Server 2010 must be wondering what’s so new and great about PowerShell 2.0. In a series of articles, we’ll look at that from several angles – Exchange, Windows administration, and … well, we’ll start our journey with a language tack.

In the midst of that Get-RandomName script, we had a code snippet to pick a random first name, either male or female, from one of two distinct lists (arrays).

if( $rand.NextDouble() -lt $percentFemale ){

$gender = “male”

}

else{

$gender = “female”

}

$f = (Invoke-Expression (“`$_RN_{0}[`$rand.Next(`$_RN_{0}.Count)]” -f $gender)).Split(“`t”)[0].ToLower()

In PowerShell 1.0, the value of statements such as if or switch cannot be evaluated. PowerShell 2.0 changes this, which might not mean much to you if you aren’t prone to take the value of an if or switch statement, but… well, keep reading as we look at two variations on the above code.

First, in PowerShell 2.0, we can take the value of the if statement and therefore assign the variable $gender to the value of the if or else clause as follows.

$gender = if( $rand.NextDouble() -lt $percentFemale ){“male”}else{“female”}

That could of course still be written across six lines instead of one, however the important distinction is that we’ve specified the $gender = before the if, and not within each of the code blocks for the if and else clauses. If you’re a programmer familiar with the question-mark + colon operator in languages such as C, C++, and C#, then taking the value of an if or switch statement might seem ordinary. If you’re used to PowerShell 1.0, it might seem bizarre, but PowerShell 2.0 let’s us do this and more.

So riddle me this. If we can take the value of an if statement an assign a variable to the result, can we simply inject the whole shebang ($shebang = if( … ){ … }else{ … }) into the middle of another expression? Oh yes. Yes, we can. As such – noting that the following was written on one line in the modified Get-RandomName script which is only PowerShell 2.0 compatible.

$f = (Invoke-Expression (“`$_RN_{0}[`$rand.Next(`$_RN_{0}.Count)]” -f $(if( $rand.NextDouble() -lt

$percentFemale ){“male”}else{“female”}))).Split(“`t”)[0].ToLower()

The important “extra” syntax used to properly insert the if statement into the expression is to use $() around it.

PowerShell 2.0 has much more expressiveness as a scripting and programming language than PowerShell 1.0 had, making it that much more powerful in the arena of languages such as Icon and LISP. Yes, true fans, now with PowerShell 2.0 you can take the value of a switch block in an expression as well. “If” is just the beginning.

This is your brain on PowerShell… any questions?

Leave a Reply

Your email address will not be published. Required fields are marked *