5

I recently started using PowerShell, and noticed that I could pass argument values using a space between the argument name and value, or using a colon, like this:

MyFunction -Parameter value
or
MyFunction -Parameter:value

I started using the colon because it differentiates the line a bit more (for readability reasons), but from what I've seen, most people do not use it.

I've read a bit also about the differences between these approaches when working with switch typed arguments, that normally do not need values. In that situation, one needs to use the colon, or else the command will not work. This is another reason why I'm leaning towards using the colon for every parameter, for consistency reasons.

Is there something else I should keep in mind?

Community
  • 1
  • 1
julealgon
  • 7,072
  • 3
  • 32
  • 77
  • 1
    possible duplicate of [Powershell: Colon in commandlet parameters](http://stackoverflow.com/questions/8525572/powershell-colon-in-commandlet-parameters) – Matt Oct 31 '14 at 13:09
  • @Matt This is not a duplicate at all. I even linked that post in my OP. I'm fully aware of the why it's used in that context, my question is if I should adopt it for all arguments or not, really. – julealgon Oct 31 '14 at 13:54
  • 1
    My fault for not following the link. The title was different. The post showed the only real reason to use it. If that is the case it would boil down to a matter of opinion. Must like the answers no one else uses it out of that context – Matt Oct 31 '14 at 13:59

2 Answers2

4

Generally speaking, when I need to execute a function with a switch parameter set to false, I simply omit the switch. That's the design intent of a switch parameter. The only time I think I would ever use the colon in a parameter is when I need to programmatically determine the value of a switch.

For example, let's say I need to get a regular directory listing on even days, and a recursive directory listing on odd days:

Get-ChildItem -Path $Path -Recurse:$((Get-Date).Day % 2 -eq 1) | ForEach-Object {...}

Beyond that, I personally wouldn't bother with the colon unless it significantly added to the readability of a given statement. It's not a commonly used syntax, so people who read your code later are more likely to be confused by it.

Bacon Bits
  • 30,782
  • 5
  • 59
  • 66
  • "_when I need to execute a function with a switch parameter set to false, I simply omit the switch_" Just recall another "programmatic" use case: if you're calling a function with a switch parameter from another function where the intended switch value is in a variable. Now you _have_ to use `:`. ie, if `func1` has a switch parameter `$debug` & `func2` _also_ has a switch param `$debug`, & `func1` calls `func2` (& wants to pass the same `$debug` value), you have to call `func2 -debug:$debug`. And then you're in OP land, where I'm leaning towards `:` everywhere (in functions at least) too. YMMV – ruffin Dec 20 '21 at 16:11
2

In general I would leave the colon off. Only use it in the situation of setting switch a parameter (typically when you want to pass a variable to it, like -Verbose:$someVariable.

Basically, I think you should be consistent with the more accepted style, which is what I've described.

If you really want to set all parameters in an internally consistent way which allows for variables for switch parameters, and is an accepted (though less known) way of passing parameters, consider splatting like so:

$params = @{
    'Param1' = $value1
    'Param2' = 5
    'WhatIf' = $true
}

Some-Cmdlet @params
briantist
  • 45,546
  • 6
  • 82
  • 127