Should I use colons for all arguments to the / script function call?

I recently started using PowerShell and noticed that I can pass argument values ​​using a space between the name and value of the argument or using a colon, for example:

MyFunction -Parameter value
or
MyFunction -Parameter:value

I started using the colon because it distinguishes the line a bit more (for readability), but from what I saw, most people don't use it.

I also read a little about the differences between these approaches when dealing with switchtyped arguments that usually don't need values. In this situation, you need to use a colon, otherwise the command will not work. This is another reason why I tend to use a colon for each parameter for reasons of consistency.

Is there anything else I should remember?

+4
source share
2 answers

Generally speaking, when I need to execute a function with the switch parameter set to false, I simply omit the switch. This is the design intent of the switch parameter. The only time I think I will ever use a 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 list of directories on even days and a recursive list of directories on odd days:

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

, , . , , , .

+4

, . (, , -Verbose:$someVariable.

, , , .

, ( ) , splatting :

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

Some-Cmdlet @params
+2

All Articles