Bird
0
0

Which of the following is the correct syntax to declare an advanced function with CmdletBinding and a mandatory parameter Name?

easy📝 Syntax Q12 of 15
PowerShell - Functions
Which of the following is the correct syntax to declare an advanced function with CmdletBinding and a mandatory parameter Name?
Afunction Get-User { [CmdletBinding()] param([Parameter(Mandatory=$true)][string]$Name) }
Bfunction Get-User { param([string]$Name) Mandatory }
Cfunction Get-User { param([Parameter(Mandatory=$true)][string]$Name) [CmdletBinding()] }
Dfunction Get-User { param([string]$Name) [CmdletBinding()] }
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct CmdletBinding placement

    [CmdletBinding()] must appear before param() block in the function.
  2. Step 2: Check parameter declaration

    Mandatory parameter requires [Parameter(Mandatory=$true)] attribute inside param(). function Get-User { [CmdletBinding()] param([Parameter(Mandatory=$true)][string]$Name) } correctly places both.
  3. Final Answer:

    function Get-User { [CmdletBinding()] param([Parameter(Mandatory=$true)][string]$Name) } -> Option A
  4. Quick Check:

    CmdletBinding before param, mandatory attribute inside param = D [OK]
Quick Trick: Put [CmdletBinding()] before param() with attributes inside param [OK]
Common Mistakes:
  • Placing CmdletBinding after param
  • Missing Mandatory attribute syntax
  • Using 'Mandatory' keyword alone

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes