Introduction
Default parameter values let you set a fallback value for a function's input. This means the function still works even if you don't give it all the information.
function FunctionName { param( [type]$ParameterName = DefaultValue ) # Function code here }
function Greet { param( [string]$Name = "Friend" ) Write-Output "Hello, $Name!" } Greet Greet -Name "Alice"
function Add-Numbers { param( [int]$a = 5, [int]$b = 10 ) return ($a + $b) } Add-Numbers Add-Numbers -a 3 -b 7
function Show-Message { param( [string]$Message = "Hello, World!" ) Write-Output $Message } Show-Message Show-Message -Message "PowerShell is fun!"
default parameter values in a PowerShell function?$count in a PowerShell function?= correctly to assign 10 as default.function Greet { param([string]$name = "Guest") "Hello, $name!" }
Greet$name has a default value "Guest" if no argument is passed.Greet without arguments uses the default "Guest".function Show-Info {
param([string]$info =)
Write-Output $info
}$info has an equals sign but no value after it, which is invalid.Calculate-Sum that adds two numbers, but if the second number is not provided, it should default to the first number. Which parameter default setup achieves this?param([int]$a, [int]$b = $a) sets $b to the value of $a when $b is not provided.Pass the test and this pattern moves unknown