What does the Mandatory attribute do in a PowerShell parameter?
The Mandatory attribute makes a parameter required. PowerShell will ask the user to provide a value if it is not supplied when running the script or function.
Click to reveal answer
beginner
What is the purpose of the ValidateSet attribute in PowerShell parameters?
The ValidateSet attribute restricts the parameter's input to a predefined list of allowed values. This helps prevent errors by limiting choices to valid options.
Click to reveal answer
intermediate
How do you define a mandatory parameter named Color that only accepts 'Red', 'Green', or 'Blue'?
Use [Parameter(Mandatory=$true)][ValidateSet('Red','Green','Blue')]$Color in the parameter block to make it required and limit choices.
Click to reveal answer
beginner
What happens if a user provides a value not in the ValidateSet list?
PowerShell throws an error and stops execution, telling the user the value is invalid and showing the allowed options.
Click to reveal answer
beginner
Can you combine Mandatory and ValidateSet attributes on the same parameter?
Yes, combining them ensures the parameter is required and only accepts specific values from the set.
Click to reveal answer
What does the Mandatory attribute do in a PowerShell parameter?
AValidates the parameter type
BLimits the parameter to specific values
CMakes the parameter optional
DMakes the parameter required to be provided
✗ Incorrect
The Mandatory attribute forces the user to provide a value for the parameter.
Which attribute restricts a parameter's input to a fixed list of values?
AMandatory
BValidateRange
CValidateSet
DParameter
✗ Incorrect
ValidateSet limits input to a predefined set of allowed values.
What will happen if a user omits a parameter marked as Mandatory?
APowerShell will assign a default value
BPowerShell will prompt the user to enter a value
CPowerShell will ignore the parameter
DPowerShell will throw an error immediately
✗ Incorrect
PowerShell prompts the user to enter a value for mandatory parameters if missing.
If a parameter has ValidateSet('Red','Green','Blue'), what happens if the user inputs 'Yellow'?
APowerShell throws an error about invalid value
BPowerShell ignores the parameter
CPowerShell converts 'Yellow' to 'Red'
DThe script runs normally
✗ Incorrect
PowerShell throws an error because 'Yellow' is not in the allowed set.
How do you make a parameter both required and limited to specific values?
AUse both <code>[Parameter(Mandatory=$true)]</code> and <code>[ValidateSet()]</code>
BUse <code>[ValidateSet()]</code> only
CUse <code>[Parameter(Mandatory=$true)]</code> only
DUse <code>[MandatorySet()]</code>
✗ Incorrect
Combining both attributes makes the parameter required and restricts its values.
Explain how the Mandatory and ValidateSet attributes work together in PowerShell parameters.
Think about forcing the user to choose from specific options.
You got /3 concepts.
Describe what happens when a user runs a script without providing a mandatory parameter that has a ValidateSet.
Consider the user experience when input is missing or wrong.
You got /3 concepts.
Practice
(1/5)
1. What does the [Parameter(Mandatory=$true)] attribute do in a PowerShell script parameter?
easy
A. It limits the parameter to specific allowed values.
B. It sets a default value for the parameter.
C. It makes the parameter optional.
D. It makes the parameter required when running the script.
Solution
Step 1: Understand the Mandatory attribute
The Mandatory=$true setting means the parameter must be provided by the user when running the script.
Step 2: Differentiate from other attributes
Unlike ValidateSet, which restricts values, Mandatory only requires the parameter to be present.
Final Answer:
It makes the parameter required when running the script. -> Option D
Quick Check:
Mandatory = required parameter [OK]
Hint: Mandatory means you must provide the parameter value [OK]
Common Mistakes:
Confusing Mandatory with ValidateSet
Thinking Mandatory sets default values
Assuming Mandatory makes parameter optional
2. Which of the following is the correct syntax to restrict a mandatory parameter to only accept the values 'Red', 'Green', or 'Blue' in PowerShell?
easy
A. [Parameter(Mandatory=$true)][ValidateSet('Red','Green','Blue')]$Color
B. [ValidateSet('Red','Green','Blue')][Parameter()]$Color
C. [Parameter()][ValidateSet('Red','Green','Blue')]$Color
D. [ValidateSet('Red','Green','Blue')]$Color
Solution
Step 1: Check attribute order and syntax
Both Parameter and ValidateSet attributes can be used together. The order does not affect functionality, but the common style is to put Parameter first.
Step 2: Confirm Mandatory and ValidateSet usage
[Parameter(Mandatory=$true)][ValidateSet('Red','Green','Blue')]$Color correctly uses Mandatory=$true and ValidateSet with the allowed values.
Final Answer:
[Parameter(Mandatory=$true)][ValidateSet('Red','Green','Blue')]$Color -> Option A
But when running the script without the -Size argument, it does not prompt for input and continues silently. What is the likely cause?
medium
A. The parameter type should be [int] instead of [string].
B. The script is missing CmdletBinding() to enable parameter validation.
C. Mandatory parameters never prompt for input automatically.
D. The ValidateSet attribute is incorrect and disables Mandatory.
Solution
Step 1: Understand Mandatory prompt behavior
Mandatory parameters prompt for input only if the function or script uses CmdletBinding() or is an advanced function.
Step 2: Identify missing CmdletBinding()
Without CmdletBinding(), the script treats parameters as simple variables and does not enforce Mandatory prompts.
Final Answer:
The script is missing CmdletBinding() to enable parameter validation. -> Option B
Quick Check:
CmdletBinding() enables Mandatory prompts [OK]
Hint: Add CmdletBinding() to enable Mandatory prompts [OK]
Common Mistakes:
Thinking ValidateSet disables Mandatory
Assuming Mandatory never prompts
Changing parameter type unnecessarily
5. You want to create a script parameter that is required and only accepts the values 'Start', 'Stop', or 'Restart'. Which of the following parameter declarations correctly enforces both requirements and will prompt the user if the parameter is missing?
hard
A. [CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[ValidateSet('Start','Stop','Restart')]
[string]$Action
)
B. param(
[Parameter()]
[ValidateSet('Start','Stop','Restart')]
[string]$Action = 'Start'
)
C. [CmdletBinding()]
param(
[Parameter()]
[ValidateSet('Start','Stop','Restart')]
[string]$Action
)
D. param(
[Parameter(Mandatory=$true)]
[string]$Action
)
Solution
Step 1: Ensure Mandatory prompts user
To prompt the user when a parameter is missing, the script must use CmdletBinding() and set Mandatory=$true.
Step 2: Restrict values with ValidateSet
Using ValidateSet limits input to the allowed values 'Start', 'Stop', or 'Restart'.
Step 3: Check all options
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[ValidateSet('Start','Stop','Restart')]
[string]$Action
) includes CmdletBinding(), Mandatory=$true, and ValidateSet, fulfilling all requirements.
Final Answer:
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[ValidateSet('Start','Stop','Restart')]
[string]$Action
) -> Option A