Bird
Raised Fist0
PowerShellscripting~20 mins

Parameters in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the purpose of using param() in a PowerShell script?
easy
A. To declare input parameters that the script can accept
B. To define a function inside the script
C. To output results from the script
D. To comment lines in the script

Solution

  1. Step 1: Understand the role of param()

    The param() block is used at the start of a PowerShell script to declare parameters that the script can accept as input.
  2. Step 2: Differentiate from other script parts

    It is not used to define functions, output results, or comment lines. Those have different syntax and purposes.
  3. Final Answer:

    To declare input parameters that the script can accept -> Option A
  4. Quick Check:

    param() declares input parameters [OK]
Hint: Remember: param() is for input, not output or comments [OK]
Common Mistakes:
  • Confusing param() with function declaration
  • Thinking param() outputs data
  • Using param() to comment code
2. Which of the following is the correct syntax to declare a mandatory string parameter named Name in PowerShell?
easy
A. param([string]$Name = "")
B. param([string]$Name)
C. param($Name:string Mandatory)
D. param([Parameter(Mandatory=$true)][string]$Name)

Solution

  1. Step 1: Understand mandatory parameter syntax

    To make a parameter mandatory, use the [Parameter(Mandatory=$true)] attribute before the type and name.
  2. Step 2: Check each option

    param([Parameter(Mandatory=$true)][string]$Name) correctly uses [Parameter(Mandatory=$true)][string]$Name. param([string]$Name = "") sets a default empty string, so not mandatory. param([string]$Name) lacks the mandatory attribute. param($Name:string Mandatory) is invalid syntax.
  3. Final Answer:

    param([Parameter(Mandatory=$true)][string]$Name) -> Option D
  4. Quick Check:

    Mandatory parameter needs [Parameter(Mandatory=$true)] [OK]
Hint: Mandatory parameters use [Parameter(Mandatory=$true)] attribute [OK]
Common Mistakes:
  • Omitting the Mandatory attribute
  • Using default value instead of mandatory
  • Incorrect attribute syntax
3. What will be the output of this script when run with -Count 3?
param([int]$Count = 1)
for ($i = 1; $i -le $Count; $i++) { Write-Output "Number $i" }
medium
A. Number 1 Number 2 Number 3
B. Number 1
C. Number 3
D. Error: Missing parameter

Solution

  1. Step 1: Understand parameter default and input

    The parameter $Count has a default of 1 but is overridden by passing -Count 3.
  2. Step 2: Trace the loop output

    The for loop runs from 1 to 3, outputting "Number 1", "Number 2", and "Number 3" each on a new line.
  3. Final Answer:

    Number 1 Number 2 Number 3 -> Option A
  4. Quick Check:

    Loop runs 3 times with Count=3 [OK]
Hint: Parameter overrides default; loop runs Count times [OK]
Common Mistakes:
  • Ignoring passed parameter and using default
  • Expecting single output instead of looped output
  • Confusing parameter name or type
4. Identify the error in this parameter declaration:
param([int]$Age = "twenty")
medium
A. Missing mandatory attribute
B. Type mismatch: assigning string to int parameter
C. Parameter name cannot be Age
D. Default value must be null

Solution

  1. Step 1: Check parameter type and default value

    The parameter $Age is declared as [int] but the default value is the string "twenty".
  2. Step 2: Understand type mismatch error

    PowerShell cannot convert the string "twenty" to an integer, causing a type mismatch error.
  3. Final Answer:

    Type mismatch: assigning string to int parameter -> Option B
  4. Quick Check:

    Default value must match parameter type [OK]
Hint: Default values must match declared parameter type [OK]
Common Mistakes:
  • Assigning string default to int parameter
  • Ignoring type mismatch errors
  • Assuming parameter name restrictions
5. You want to create a script that accepts two parameters: Path (string, mandatory) and Verbose (switch, optional). Which is the correct way to declare these parameters?
hard
A. param([string]$Path, [Parameter(Mandatory=$true)][switch]$Verbose)
B. param([string]$Path = "", [switch]$Verbose = $false)
C. param([Parameter(Mandatory=$true)][string]$Path, [switch]$Verbose)
D. param([switch]$Path, [string]$Verbose)

Solution

  1. Step 1: Identify mandatory and optional parameters

    Path must be mandatory string, so use [Parameter(Mandatory=$true)][string]$Path. Verbose is an optional switch, so [switch]$Verbose without mandatory attribute.
  2. Step 2: Evaluate each option

    param([Parameter(Mandatory=$true)][string]$Path, [switch]$Verbose) correctly declares Path as mandatory string and Verbose as optional switch. param([string]$Path, [Parameter(Mandatory=$true)][switch]$Verbose) wrongly makes Verbose mandatory. param([string]$Path = "", [switch]$Verbose = $false) sets default empty string for Path, so not mandatory. param([switch]$Path, [string]$Verbose) swaps types incorrectly.
  3. Final Answer:

    param([Parameter(Mandatory=$true)][string]$Path, [switch]$Verbose) -> Option C
  4. Quick Check:

    Mandatory string + optional switch = param([Parameter(Mandatory=$true)][string]$Path, [switch]$Verbose) [OK]
Hint: Mandatory uses [Parameter(Mandatory=$true)], switches don't [OK]
Common Mistakes:
  • Making switches mandatory
  • Setting default values instead of mandatory
  • Mixing parameter types
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard