Challenge - 5 Problems
PowerShell Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this PowerShell script with parameters?
Consider this script saved as
TestParams.ps1 and run with .\TestParams.ps1 -Name 'Alice' -Age 30. What will it output?PowerShell
param(
[string]$Name,
[int]$Age
)
Write-Output "Name: $Name, Age: $Age"Attempts:
2 left
💡 Hint
Look at how parameters are declared and passed in PowerShell scripts.
✗ Incorrect
The script defines two parameters, Name and Age. When run with -Name 'Alice' and -Age 30, these values are assigned correctly and printed.
📝 Syntax
intermediate2:00remaining
Which option correctly declares a mandatory string parameter in PowerShell?
You want to declare a parameter named
FilePath that is mandatory and must be a string. Which declaration is correct?Attempts:
2 left
💡 Hint
Look for the correct syntax of the Parameter attribute.
✗ Incorrect
Option B uses the correct syntax with the Parameter attribute and Mandatory property set to $true.
🔧 Debug
advanced2:00remaining
Why does this script fail when calling with no arguments?
Given this script snippet, what error occurs when run without arguments?
param(
[Parameter(Mandatory=$true)]
[string]$User
)
Write-Output "User is $User"
PowerShell
param(
[Parameter(Mandatory=$true)]
[string]$User
)
Write-Output "User is $User"Attempts:
2 left
💡 Hint
Think about what happens when a mandatory parameter is not provided.
✗ Incorrect
Mandatory parameters require a value. If none is given, PowerShell throws an error indicating the missing argument.
🚀 Application
advanced2:00remaining
What is the output when using a default parameter value?
Consider this script run as
.\Script.ps1 with no arguments. What will it output?
param(
[string]$City = "Seattle"
)
Write-Output "City is $City"PowerShell
param(
[string]$City = "Seattle"
)
Write-Output "City is $City"Attempts:
2 left
💡 Hint
Check how default values work for parameters.
✗ Incorrect
If no argument is passed, the parameter uses its default value, here 'Seattle'.
🧠 Conceptual
expert2:00remaining
How many parameters does this script accept and what are their types?
Analyze this parameter block and answer how many parameters are declared and their types:
param(
[string]$Name,
[int]$Count = 5,
[switch]$Verbose
)
Write-Output "$Name, $Count, $Verbose"
PowerShell
param(
[string]$Name,
[int]$Count = 5,
[switch]$Verbose
)
Write-Output "$Name, $Count, $Verbose"Attempts:
2 left
💡 Hint
Look carefully at the param block and types.
✗ Incorrect
The script declares three parameters: Name as string, Count as int with default 5, and Verbose as a switch (boolean flag).