You run the script by passing parameters like: .\script.ps1 -UserName Alice -Age 30.
If you don't provide a parameter value and no default is set, PowerShell will ask you to enter it.
Using parameters makes your scripts more reusable and easier to maintain.
Summary
Parameters let scripts accept input values to work with.
Declare parameters at the start using param().
You can set types and default values for parameters.
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
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.
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.
Final Answer:
To declare input parameters that the script can accept -> Option A
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
Step 1: Understand mandatory parameter syntax
To make a parameter mandatory, use the [Parameter(Mandatory=$true)] attribute before the type and name.
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.
Final Answer:
param([Parameter(Mandatory=$true)][string]$Name) -> Option D
The parameter $Count has a default of 1 but is overridden by passing -Count 3.
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.
Final Answer:
Number 1
Number 2
Number 3 -> Option A
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
Step 1: Check parameter type and default value
The parameter $Age is declared as [int] but the default value is the string "twenty".
Step 2: Understand type mismatch error
PowerShell cannot convert the string "twenty" to an integer, causing a type mismatch error.
Final Answer:
Type mismatch: assigning string to int parameter -> Option B
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
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.
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.
Final Answer:
param([Parameter(Mandatory=$true)][string]$Path, [switch]$Verbose) -> Option C