Bird
Raised Fist0
PowerShellscripting~20 mins

Advanced functions (CmdletBinding) in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
CmdletBinding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this advanced function call?
Consider this PowerShell advanced function with CmdletBinding. What will be the output when calling Get-Greeting -Name 'Alice'?
PowerShell
function Get-Greeting {
  [CmdletBinding()]
  param(
    [Parameter(Mandatory=$true)]
    [string]$Name
  )
  Write-Output "Hello, $Name!"
}

Get-Greeting -Name 'Alice'
AHello, $Name!
BError: Missing mandatory parameter 'Name'
CHello, Alice!
DHello, !
Attempts:
2 left
💡 Hint
Look at how the parameter is used inside the function and how the function is called.
💻 Command Output
intermediate
2:00remaining
What error does this advanced function produce?
What error will this PowerShell advanced function produce when called without parameters?
PowerShell
function Test-Param {
  [CmdletBinding()]
  param(
    [Parameter(Mandatory=$true)]
    [string]$InputValue
  )
  Write-Output $InputValue
}

Test-Param
AError: Missing mandatory parameter 'InputValue'
BNo output, function runs silently
CError: Unexpected token 'Test-Param'
DOutput: null
Attempts:
2 left
💡 Hint
Mandatory parameters require a value when calling the function.
📝 Syntax
advanced
2:00remaining
Which option correctly defines an advanced function with CmdletBinding and a switch parameter?
Select the correct PowerShell function definition that uses CmdletBinding and includes a switch parameter named 'VerboseMode'.
A
function Show-Info {
  [CmdletBinding()]
  param(
    [switch]$VerboseMode
  )
  if ($VerboseMode) { Write-Output 'Verbose mode on' }
}
B
function Show-Info {
  param(
    [CmdletBinding()]
    [switch]$VerboseMode
  )
  if ($VerboseMode) { Write-Output 'Verbose mode on' }
}
C
function Show-Info {
  [CmdletBinding]
  param(
    [switch]$VerboseMode
  )
  if ($VerboseMode) { Write-Output 'Verbose mode on' }
}
D
function Show-Info {
  [CmdletBinding()]
  param(
    switch $VerboseMode
  )
  if ($VerboseMode) { Write-Output 'Verbose mode on' }
}
Attempts:
2 left
💡 Hint
CmdletBinding must be an attribute before param block, and switch parameters require [switch] type.
🚀 Application
advanced
2:00remaining
What is the output of this advanced function using Begin, Process, and End blocks?
Given this advanced function, what will be the output when calling Process-Numbers -Numbers 1,2,3?
PowerShell
function Process-Numbers {
  [CmdletBinding()]
  param(
    [int[]]$Numbers
  )
  begin {
    Write-Output 'Starting processing'
  }
  process {
    foreach ($num in $Numbers) {
      Write-Output ($num * 2)
    }
  }
  end {
    Write-Output 'Processing complete'
  }
}

Process-Numbers -Numbers 1,2,3
A[1, 2, 3]
B[2, 4, 6]
C[Starting processing, Processing complete]
D[Starting processing, 2, 4, 6, Processing complete]
Attempts:
2 left
💡 Hint
Begin runs once before processing, Process runs for each input, End runs after all processing.
🔧 Debug
expert
3:00remaining
Why does this advanced function fail to bind the parameter correctly?
This function is intended to accept pipeline input by property name. Why does it fail to bind the parameter when called with pipeline input?
PowerShell
function Get-UserName {
  [CmdletBinding()]
  param(
    [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
    [string]$UserName
  )
  process {
    Write-Output "User: $UserName"
  }
}

# Pipeline input
$users = @(@{UserName='Bob'}, @{UserName='Carol'})
$users | Get-UserName
AThe parameter name 'UserName' does not match the property name in the pipeline objects.
BThe pipeline objects are hashtables, which do not support property binding as expected.
CThe function does not declare the parameter as accepting pipeline input by property name correctly.
DThe function lacks a 'begin' block to initialize pipeline processing.
Attempts:
2 left
💡 Hint
Consider how PowerShell binds pipeline input by property name and the type of objects passed.

Practice

(1/5)
1. What is the main purpose of using [CmdletBinding()] in a PowerShell function?
easy
A. To define global variables inside the function
B. To automatically run the function in the background
C. To disable parameter validation
D. To make the function behave like a built-in cmdlet with advanced features

Solution

  1. Step 1: Understand CmdletBinding purpose

    [CmdletBinding()] adds cmdlet-like features such as common parameters and better error handling.
  2. Step 2: Compare options

    Options A, C, and D do not describe what CmdletBinding does. It does not define globals, disable validation, or run background tasks.
  3. Final Answer:

    To make the function behave like a built-in cmdlet with advanced features -> Option D
  4. Quick Check:

    CmdletBinding enables cmdlet features = B [OK]
Hint: CmdletBinding makes functions act like real cmdlets [OK]
Common Mistakes:
  • Thinking CmdletBinding defines variables
  • Assuming it disables validation
  • Believing it runs functions in background
2. Which of the following is the correct syntax to declare an advanced function with CmdletBinding and a mandatory parameter Name?
easy
A. function Get-User { [CmdletBinding()] param([Parameter(Mandatory=$true)][string]$Name) }
B. function Get-User { param([string]$Name) Mandatory }
C. function Get-User { param([Parameter(Mandatory=$true)][string]$Name) [CmdletBinding()] }
D. function Get-User { param([string]$Name) [CmdletBinding()] }

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]
Hint: Put [CmdletBinding()] before param() with attributes inside param [OK]
Common Mistakes:
  • Placing CmdletBinding after param
  • Missing Mandatory attribute syntax
  • Using 'Mandatory' keyword alone
3. What will be the output of this PowerShell advanced function when called as Get-Greeting -Name 'Alice'?
function Get-Greeting {
  [CmdletBinding()]
  param(
    [Parameter(Mandatory=$true)]
    [string]$Name
  )
  Write-Output "Hello, $Name!"
}
medium
A. "Hello, Alice!"
B. Error: Missing mandatory parameter
C. "Hello, $Name!"
D. No output

Solution

  1. Step 1: Understand parameter passing

    The function requires a mandatory string parameter Name, which is provided as 'Alice'.
  2. Step 2: Evaluate output statement

    Write-Output uses double quotes, so $Name is replaced by 'Alice', producing "Hello, Alice!".
  3. Final Answer:

    "Hello, Alice!" -> Option A
  4. Quick Check:

    Mandatory parameter passed, string interpolation works = C [OK]
Hint: Double quotes expand variables, mandatory param given = output shown [OK]
Common Mistakes:
  • Confusing single vs double quotes
  • Expecting error despite providing parameter
  • Thinking variable won't expand
4. Identify the error in this advanced function code snippet:
function Test-Function {
  param(
    [Parameter(Mandatory=$true)]
    [string]$InputValue
  )
  [CmdletBinding()]
  Write-Output $InputValue
}
medium
A. Write-Output cannot be used inside functions
B. Mandatory attribute cannot be used with strings
C. [CmdletBinding()] must be before param() block
D. Function name cannot contain hyphens

Solution

  1. Step 1: Check CmdletBinding placement

    [CmdletBinding()] must appear before the param() block, but here it is after.
  2. Step 2: Validate other parts

    Mandatory attribute is valid for strings, Write-Output is allowed, and hyphens in function names are allowed.
  3. Final Answer:

    [CmdletBinding()] must be before param() block -> Option C
  4. Quick Check:

    CmdletBinding before param required = A [OK]
Hint: CmdletBinding always goes before param() [OK]
Common Mistakes:
  • Placing CmdletBinding after param
  • Thinking Mandatory is invalid for strings
  • Believing Write-Output is disallowed
5. You want to create an advanced function that accepts a switch parameter -Verbose to show extra messages only when the switch is used. Which code snippet correctly implements this behavior?
hard
A. function Show-Info { [CmdletBinding()] param() if ($Verbose) { Write-Output 'Verbose mode ON' } else { Write-Output 'Normal mode' } }
B. function Show-Info { [CmdletBinding()] param([switch]$Verbose) if ($PSBoundParameters.ContainsKey('Verbose')) { Write-Output 'Verbose mode ON' } else { Write-Output 'Normal mode' } }
C. function Show-Info { param([switch]$Verbose) [CmdletBinding()] if ($Verbose) { Write-Output 'Verbose mode ON' } else { Write-Output 'Normal mode' } }
D. function Show-Info { [CmdletBinding()] param([bool]$Verbose) if ($Verbose -eq $true) { Write-Output 'Verbose mode ON' } else { Write-Output 'Normal mode' } }

Solution

  1. Step 1: Confirm CmdletBinding and param order

    [CmdletBinding()] must come before param(). Only options A, B, and D do this correctly; C is wrong.
  2. Step 2: Check switch parameter usage

    Switch parameters are declared as [switch]$Verbose. To detect if switch was used, check $PSBoundParameters.ContainsKey('Verbose') or $Verbose directly. function Show-Info { [CmdletBinding()] param([switch]$Verbose) if ($PSBoundParameters.ContainsKey('Verbose')) { Write-Output 'Verbose mode ON' } else { Write-Output 'Normal mode' } } uses $PSBoundParameters which is reliable.
  3. Step 3: Evaluate parameter type

    function Show-Info { [CmdletBinding()] param([bool]$Verbose) if ($Verbose -eq $true) { Write-Output 'Verbose mode ON' } else { Write-Output 'Normal mode' } } uses [bool] which is not the recommended type for switches; switches are special and should use [switch].
  4. Final Answer:

    function Show-Info { [CmdletBinding()] param([switch]$Verbose) if ($PSBoundParameters.ContainsKey('Verbose')) { Write-Output 'Verbose mode ON' } else { Write-Output 'Normal mode' } } -> Option B
  5. Quick Check:

    CmdletBinding before param, switch param, check $PSBoundParameters = A [OK]
Hint: Use [switch] param and check $PSBoundParameters['Verbose'] [OK]
Common Mistakes:
  • Placing CmdletBinding after param
  • Using [bool] instead of [switch]
  • Checking switch with just $Verbose without $PSBoundParameters
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard