More on PowerShell

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”



$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 shehe ($shehe = 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?


Related Courses

Configuring, Managing, and Maintaining Server 2008 (M6419)

Automating Windows Server 2008 Administration with Windows Powershell (M6434)

Implementing and Administering Windows 7 in the Enterprise

In this article

Join the Conversation


  1. Eyal Peleg Reply

    Thanks for th epost,
    Just wanted to note that your male/female is reversed.
    i.e. i expect that $percentFemale=1 means all should be females
    but it will produce all males.

    1. Brad Werner Reply

      Indeed! Thanks for noting that – sorry for the error.
      Yes, to correct this we should certainly reverse this.
      In the first form shown this should be:
      if( $rand.NextDouble() -lt $percentFemale ){
      $gender = “female”
      $gender = “male”

      And in the more “dense” PowerShell 2.0 form shown later, the correction would be:
      $f = (Invoke-Expression (“`$_RN_{0}[`$rand.Next(`$_RN_{0}.Count)]” -f $(if( $rand.NextDouble() -lt $percentFemale ){“female”}else{“male”}))).Split(“`t”)[0].ToLower()

      Again, thanks Eyal for pointing this out.