PowerShell - FunctionsWhich of these is the correct syntax to define a PowerShell function with a string parameter named 'name' that defaults to 'Guest'?Afunction SayHello { param([string]$name <- 'Guest') Write-Output "Hello, $name" }Bfunction SayHello { param([string]$name : 'Guest') Write-Output "Hello, $name" }Cfunction SayHello { param([string]$name = 'Guest') Write-Output "Hello, $name" }Dfunction SayHello { param([string]$name => 'Guest') Write-Output "Hello, $name" }Check Answer
Step-by-Step SolutionSolution:Step 1: Identify correct default parameter syntaxPowerShell uses '=' to assign default values inside param blocks.Step 2: Verify function syntaxfunction SayHello { param([string]$name = 'Guest') Write-Output "Hello, $name" } correctly uses '=' and quotes for string default value.Final Answer:function SayHello { param([string]$name = 'Guest') Write-Output "Hello, $name" } -> Option CQuick Check:Default string parameter syntax = '=' with quotes [OK]Quick Trick: Use '=' and quotes for string defaults in param [OK]Common Mistakes:Using ':' or '=>' instead of '='Missing quotes around string defaultIncorrect param block syntax
Master "Functions" in PowerShell9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More PowerShell Quizzes Functions - Advanced functions (CmdletBinding) - Quiz 10easy Functions - Advanced functions (CmdletBinding) - Quiz 12easy Functions - Why functions organize scripts - Quiz 4medium Modules and Script Organization - Module manifest (.psd1) - Quiz 8hard Modules and Script Organization - Module manifest (.psd1) - Quiz 2easy Regular Expressions - Named captures - Quiz 12easy Regular Expressions - Regex with Select-String - Quiz 13medium Regular Expressions - Named captures - Quiz 11easy Regular Expressions - Named captures - Quiz 1easy Working with Objects - Measure-Object for statistics - Quiz 10hard