Bird
0
0

You want to create a script that accepts a mandatory string parameter FilePath and an optional switch parameter Verbose. Which is the correct way to declare these parameters?

hard📝 Application Q8 of 15
PowerShell - Functions
You want to create a script that accepts a mandatory string parameter FilePath and an optional switch parameter Verbose. Which is the correct way to declare these parameters?
Aparam([Parameter(Mandatory=$true)][string]$FilePath, [switch]$Verbose)
Bparam([string]$FilePath, [switch]$Verbose = $true)
Cparam([string]$FilePath = Mandatory, [bool]$Verbose)
Dparam([Mandatory]$FilePath, [switch]$Verbose)
Step-by-Step Solution
Solution:
  1. Step 1: Understand mandatory parameter syntax

    Use the [Parameter(Mandatory=$true)] attribute to make a parameter required.
  2. Step 2: Recognize switch parameter declaration

    Switch parameters are declared with [switch] and do not need default values.
  3. Step 3: Evaluate options

    param([Parameter(Mandatory=$true)][string]$FilePath, [switch]$Verbose) correctly uses mandatory attribute and switch type; others misuse syntax or attributes.
  4. Final Answer:

    param([Parameter(Mandatory=$true)][string]$FilePath, [switch]$Verbose) -> Option A
  5. Quick Check:

    Mandatory attribute + switch type = correct declaration [OK]
Quick Trick: Use [Parameter(Mandatory=$true)] for required params [OK]
Common Mistakes:
  • Setting switch default to true
  • Using 'Mandatory' as default value
  • Omitting $ in attributes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes