OIT Quick Tip: Using PowerShell Switch Parameters in CLI Activities
Published Feb 15 2019 12:04 PM 3,076 Views
First published on TECHNET on May 04, 2012

One of the common parameter types in PowerShell is the switch parameter. It’s basically a Boolean (true/false) type of parameter, and you either add it to the command line or not. Here’s an example:


get-scoevents –formatXML

The above command does the same thing in either case, it just formats the output differently.Now if you want to be able to take a command like this and create an activity using the Command Line Activity Wizard, you’ll find that there’s no obvious way to add a switch parameter to the command line. This is because the parameters list you create only serves to do string insertion on the command line, and it can’t change the actual command line parameters used based on the user input – just the values for the parameters.

The good news is that there’s an alternate way to specify the switch parameter in the command line using an actual true or false value. The equivalent of the example lines above looks like this:

get-scoevents –formatXML:$false

get-scoevents –formatXML:$true

If you translate that into what an activity would look like, here’s the Command Line Activity Wizard view:

Note that the command line has two “$$” characters. This is because the $(TestSwitch) is added when you insert the parameter into the command line, and the extra “$” is used to make the string “$True” or “$False” to become actual boolean values.

And here’s what the script looks like:

param([Switch] $TestSwitch)
if (($TestSwitch -eq $false))  { $var1 = "The value entered was 'false'"}
elseif ($TestSwitch -eq $true) { $var1 = "The value entered was 'true'"  }

When I run the activity in the Runbook Tester with the value as True, the command line value shows up as:

C:\files\validateswitch.ps1 -TestSwitch:$True

And the “Result” property of the activity is “The value entered was 'true'”. If I run it again with the value set to False, the resulting output correctly says “The value entered was 'false'”.

So there you have it! A quick tip on using PowerShell switch parameters in your own custom activities created using the Orchestrator Command Line Activity Wizard!

Version history
Last update:
‎Mar 11 2019 09:15 AM
Updated by: