0
0
PowerShellscripting~20 mins

Parameters in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell Parameters 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 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"
AName: , Age:
BName: , Age: 30
CName: Alice, Age: 30
DName: Alice, Age:
Attempts:
2 left
💡 Hint
Look at how parameters are declared and passed in PowerShell scripts.
📝 Syntax
intermediate
2: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?
Aparam([string]$FilePath Mandatory=$true)
Bparam([Parameter(Mandatory=$true)][string]$FilePath)
Cparam([Mandatory] [string]$FilePath)
Dparam([string]$FilePath -Mandatory)
Attempts:
2 left
💡 Hint
Look for the correct syntax of the Parameter attribute.
🔧 Debug
advanced
2: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"
AThe script throws an error: Missing an argument for the mandatory parameter 'User'.
BThe script throws a syntax error due to missing default value.
CThe script outputs: User is
DThe script runs but outputs 'User is $User' literally.
Attempts:
2 left
💡 Hint
Think about what happens when a mandatory parameter is not provided.
🚀 Application
advanced
2: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"
AError: Parameter 'City' requires a value.
BCity is
CCity is $City
DCity is Seattle
Attempts:
2 left
💡 Hint
Check how default values work for parameters.
🧠 Conceptual
expert
2: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"
AThree parameters: Name (string), Count (int with default 5), Verbose (switch)
BTwo parameters: Name (string), Count (int), Verbose is not a parameter
CThree parameters: Name (string), Count (string), Verbose (boolean)
DOne parameter: Name (string), Count and Verbose are variables
Attempts:
2 left
💡 Hint
Look carefully at the param block and types.