What if you could tell your script exactly what to do each time without rewriting it?
Why Parameters in PowerShell? - Purpose & Use Cases
Imagine you have a script that renames files. Every time you want to rename a different file or use a different name, you open the script and change the file path and new name inside the code manually.
This manual method is slow and boring. You might forget to change all places, make typos, or accidentally overwrite the wrong file. It's like rewriting your grocery list every time you shop instead of just telling someone what you need.
Parameters let you give your script inputs from outside, like telling a friend exactly what to do without changing the instructions inside. You just say the file name and new name when you run the script, making it flexible and safe.
Rename-Item -Path 'C:\oldname.txt' -NewName 'newname.txt'
param($Path, $NewName) Rename-Item -Path $Path -NewName $NewName
Parameters make your scripts reusable and adaptable, so you can run the same script many times with different inputs easily.
Think of a script that backs up files. With parameters, you can tell it which folder to back up and where to save the backup each time you run it, without changing the script itself.
Parameters let you pass information into scripts without editing the code.
This saves time and reduces mistakes when running scripts repeatedly.
They make scripts flexible and easy to reuse for different tasks.
Practice
param() in a PowerShell script?Solution
Step 1: Understand the role of
Theparam()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 AQuick Check:
param() declares input parameters [OK]
- Confusing param() with function declaration
- Thinking param() outputs data
- Using param() to comment code
Name in PowerShell?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 DQuick Check:
Mandatory parameter needs [Parameter(Mandatory=$true)] [OK]
- Omitting the Mandatory attribute
- Using default value instead of mandatory
- Incorrect attribute syntax
-Count 3?
param([int]$Count = 1)
for ($i = 1; $i -le $Count; $i++) { Write-Output "Number $i" }Solution
Step 1: Understand parameter default and input
The parameter$Counthas 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 AQuick Check:
Loop runs 3 times with Count=3 [OK]
- Ignoring passed parameter and using default
- Expecting single output instead of looped output
- Confusing parameter name or type
param([int]$Age = "twenty")
Solution
Step 1: Check parameter type and default value
The parameter$Ageis 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 BQuick Check:
Default value must match parameter type [OK]
- Assigning string default to int parameter
- Ignoring type mismatch errors
- Assuming parameter name restrictions
Path (string, mandatory) and Verbose (switch, optional). Which is the correct way to declare these parameters?Solution
Step 1: Identify mandatory and optional parameters
Pathmust be mandatory string, so use[Parameter(Mandatory=$true)][string]$Path.Verboseis an optional switch, so[switch]$Verbosewithout mandatory attribute.Step 2: Evaluate each option
param([Parameter(Mandatory=$true)][string]$Path, [switch]$Verbose) correctly declaresPathas mandatory string andVerboseas optional switch. param([string]$Path, [Parameter(Mandatory=$true)][switch]$Verbose) wrongly makesVerbosemandatory. param([string]$Path = "", [switch]$Verbose = $false) sets default empty string forPath, so not mandatory. param([switch]$Path, [string]$Verbose) swaps types incorrectly.Final Answer:
param([Parameter(Mandatory=$true)][string]$Path, [switch]$Verbose) -> Option CQuick Check:
Mandatory string + optional switch = param([Parameter(Mandatory=$true)][string]$Path, [switch]$Verbose) [OK]
- Making switches mandatory
- Setting default values instead of mandatory
- Mixing parameter types
Ready to prove it?
Pass the test and this pattern moves unknown
