0
0
PowerShellscripting~20 mins

Parameter attributes (Mandatory, ValidateSet) in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Parameter Attributes Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a script with Mandatory and ValidateSet attributes
What will be the output when running this PowerShell script without any parameters?
PowerShell
function Test-Param {
  param(
    [Parameter(Mandatory=$true)]
    [ValidateSet('Red','Green','Blue')]
    [string]$Color
  )
  "Selected color is $Color"
}

Test-Param
ASelected color is Green
BError: The value provided is not in the ValidateSet.
CYou must provide a value for the mandatory parameter 'Color'.
DSelected color is Red
Attempts:
2 left
💡 Hint
Think about what happens when a mandatory parameter is not provided.
💻 Command Output
intermediate
2:00remaining
Output when passing invalid value to ValidateSet parameter
What happens when you run this script with the parameter value 'Yellow'?
PowerShell
function Test-Param {
  param(
    [Parameter(Mandatory=$true)]
    [ValidateSet('Red','Green','Blue')]
    [string]$Color
  )
  "Selected color is $Color"
}

Test-Param -Color Yellow
ASelected color is Yellow
BError: Cannot validate argument on parameter 'Color'. The argument 'Yellow' does not belong to the set 'Red,Green,Blue'.
CSelected color is Red
DNo output, script runs silently
Attempts:
2 left
💡 Hint
Check what ValidateSet does when an invalid value is passed.
📝 Syntax
advanced
2:00remaining
Identify the correct syntax for Mandatory and ValidateSet attributes
Which option correctly declares a parameter named 'Mode' that is mandatory and accepts only 'Auto', 'Manual', or 'Off'?
A[Parameter(Mandatory=$true)][ValidateSet('Auto','Manual','Off')][string]$Mode
B[ValidateSet('Auto','Manual','Off')][Parameter(Mandatory=$true)][string]$Mode
CedoM$]gnirts[])'ffO','launaM','otuA'(teSetadilaV[])eurt$=yrotadnaM(retemaraP[
D[Parameter(Mandatory=$false)][ValidateSet('Auto','Manual','Off')][string]$Mode
Attempts:
2 left
💡 Hint
Mandatory attribute requires explicit assignment to $true or $false.
🚀 Application
advanced
2:00remaining
Determine the output of a script using Mandatory and ValidateSet with default value
What will be the output of this script when run without parameters?
PowerShell
function Test-Param {
  param(
    [Parameter(Mandatory=$true)]
    [ValidateSet('Small','Medium','Large')]
    [string]$Size = 'Medium'
  )
  "Selected size is $Size"
}

Test-Param
ASelected size is Medium
BError: Default value not allowed with Mandatory parameter.
CSelected size is Small
DYou must provide a value for the mandatory parameter 'Size'.
Attempts:
2 left
💡 Hint
Consider how Mandatory and default values interact.
🧠 Conceptual
expert
2:00remaining
Understanding behavior of Mandatory and ValidateSet in pipeline input
Given this function, what will be the output when piping the string 'Green' to it?
PowerShell
function Test-Param {
  param(
    [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
    [ValidateSet('Red','Green','Blue')]
    [string]$Color
  )
  process {
    "Selected color is $Color"
  }
}

'Green' | Test-Param
ASelected color is Green
BYou must provide a value for the mandatory parameter 'Color'.
CError: Cannot validate argument on parameter 'Color'. The argument 'Green' does not belong to the set.
DNo output
Attempts:
2 left
💡 Hint
Think about how pipeline input works with ValidateSet and Mandatory.