Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a parameter named 'Name'.
PowerShell
param([string]$[1]) Write-Output "Hello, $Name!"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different name than the variable used in the script.
Forgetting to put the parameter inside the param() block.
✗ Incorrect
The 'param' block defines parameters. Inside it, you list parameter names. Here, 'Name' is the parameter.
2fill in blank
mediumComplete the code to make the parameter 'Age' mandatory.
PowerShell
param(
[Parameter([1]=$true)]
[int]$Age
)
Write-Output "Age is $Age" Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Required' instead of 'Mandatory'.
Not using the Parameter attribute at all.
✗ Incorrect
The 'Mandatory' attribute makes a parameter required in PowerShell.
3fill in blank
hardFix the error in the parameter declaration to accept multiple strings.
PowerShell
param(
[string[]]$[1]
)
Write-Output "Items: $($Items -join ', ')" Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the variable used in the script.
Forgetting the array type [string[]].
✗ Incorrect
The variable used later is $Items, so the parameter name must be 'Items'.
4fill in blank
hardFill both blanks to define a parameter 'Count' with a default value of 5.
PowerShell
param(
[int]$[1] = [2]
)
Write-Output "Count is $Count" Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the variable used.
Setting the wrong default value.
✗ Incorrect
The parameter name is 'Count' and the default value is 5.
5fill in blank
hardFill all three blanks to create a parameter 'Verbose' that is a switch and mandatory.
PowerShell
param(
[Parameter([1]=$true)]
[[2]]$[3]
)
if ($Verbose) { Write-Output "Verbose mode on" } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Boolean instead of Switch for flags.
Not making the parameter mandatory when required.
✗ Incorrect
The parameter is mandatory, of type Switch, and named Verbose.