Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Bool vs Switch

Posted on April 30, 2010

I have to say I’m generally impressed with the quality of submissions to this year’s Scripting Games. But there is a recurring concept that some people are using and I think there’s a better way. Some contestants are defining function or script parameters as booleans. But I believe they really should be using the [switch] type. Let me show you why.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

Very often, a function has a parameter, that if true generally means do X.  Here’s a simple example.

Function Test-Bool {            
    Param([string]$r=1,[bool]$log)            
            
    $pi=[math]::pi            
    $area=$pi * ($r*$r)            
    $msg="A circle with a radius of $r has an area of $area"            
    if ($log) {            
        $msg | Tee-object -filepath $env:temp\log.txt            
        write-host "Results Logged" -ForegroundColor Green            
    }            
    else {            
        $msg            
    }            
} #end function

When the function is run, the intent is that –log should indicate to record the result. If you don’t specify –log, the script runs just fine. But if you want to use –log and indicate that yes, you do want a log, you need to explicitly provide a boolean value

PS C:\> Test-Bool –r 3 –log:$True

Here are some examples:

test-bool

Yes, technically it works but it feels clumsy in my opinion. Here’s the same function rewritten using [switch].

Function Test-Switch {            
  Param([string]$r=1,[switch]$log)            
            
    $pi=[math]::pi            
    $area=$pi * ($r*$r)            
    $msg="A circle with a radius of $r has an area of $area"            
    if ($log) {            
        $msg | Tee-object -filepath $env:temp\log.txt            
        write-host "Results Logged" -ForegroundColor Green            
    }            
    else {            
        $msg            
    }            
            
}

When used this way, if the parameter has been specified, the effect is the same as specifying $True.

PS C:\> Test-Switch –r 4 -log

 test-switch

Does this make sense? For those of you who have used [bool] as a parameter type is this more of what you had in mind?  Using [switch] seems cleaner and simpler to me. I was trying to come up with a good example of where [bool] makes sense, but I can’t come up one. If you have a use case, please share. 

Using [bool] within a script or function is a different matter. I’m merely focusing on the type when used to define a parameter.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

4 thoughts on “Bool vs Switch”

  1. Pingback: Tweets that mention Bool vs Switch | The Lonely Administrator -- Topsy.com
  2. JBandy says:
    April 30, 2010 at 2:12 pm

    I actually ran into this exact scenario writing my Adv #2 script. I did a little googling and concluded the best way was with the [switch]. Good choice it seems!

  3. Tom says:
    May 9, 2010 at 7:30 pm

    Is there a difference between the switch and a bool with a default of $false ?

    1. Jeffery Hicks says:
      May 10, 2010 at 7:09 am

      You are correct that there really isn’t a difference. However, you still need to specify the boolean value.

      function Test-A {
      Param([string]$name=”Jeff”,[bool]$test=$false)

      if ($test) {
      write-host “testing” -ForegroundColor Magenta
      }

      write-host $name
      }

      Test-A -test:$true

      As I wrote in the post, I find using Switch a bit more elegant.

      function Test-B {
      Param([string]$name=”Jeff”,[switch]$test)

      if ($test) {
      write-host “testing” -ForegroundColor Magenta
      }

      write-host $name
      }

      Test-B -test

      Or was there some other use case you had in mind?

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d