What you are doing is defining a variable in a non-parameterized function. Defining all the possibilities in a function is in the PowerShell help files.
About Functions
Switch Parameters
A switch is a parameter that does not require a value. Instead, you
type the function name followed by the name of the switch parameter.
To define a switch parameter, specify the type [switch] before the
parameter name, as shown in the following example:
function Switch-Item {
param ([switch]$on)
if ($on) { "Switch on" }
else { "Switch off" }
}
See also: About Functions Advanced for more function capabilities and About Functions Advanced Parameters.
Update as per your 'confused' comment below
Stuff like this is just an example. No matter what you call your function, you must use the parameter block to use a switch. This function could have just as easily been called ...
function MyNewFangledSwitchTag {
param ([switch]$on)
if ($on) { "Switch on" }
else { "Switch off" }
}
... and it would be the same thing, and used the same way...
MyNewFangledSwitchTag -on
Yet, never use such stuff. Always use the proper verb-noun naming convention.
Just refactor you function to be the same as the example, the name does not matter, other than using the proper conventions mentioned.
function New-MySchtsk
{
Param
(
[switch]$AtStartUp
)
if ($AtStartUp)
{
'AtStartup switch used'
# Put the rest of your code actions here
# $trigger = New-ScheduledTaskTrigger $triggers
}
else
{
'AtStartup switch was not used'
# Put the rest of your code actions here
}
}
New-MySchtsk -AtStartUp
A switch parameter is about a state. This is what the documentation tells you. You cannot assign/use anything else on it since that is not its purpose.
If the switch is used, run the code block, if it does not, do nothing, or run some other code block.
This is the same thing as using the -WhatIf switch when testing your code or any other switch available with any built-in cmdlet. You can view the Powershell Source Code on Microsoft PowerShell github to see what cmdlets do under the covers and how parameters are implemented, used, etc.