Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to set a default value for the parameter $name.
PowerShell
function Greet-User {
param(
[string]$name = [1]
)
Write-Output "Hello, $name!"
}
Greet-User
Greet-User -name "Alice" Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use quotes around the default string value.
Not using the equals sign to assign the default value.
✗ Incorrect
The default value for the parameter
$name is set to "Guest" so if no argument is passed, it uses "Guest".2fill in blank
mediumComplete the code to set a default value of 10 for the parameter $count.
PowerShell
function Repeat-Message {
param(
[int]$count = [1]
)
for ($i = 1; $i -le $count; $i++) {
Write-Output "Hello!"
}
}
Repeat-Message
Repeat-Message -count 3 Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around numbers for integer parameters.
Not assigning any default value.
✗ Incorrect
The default value for
$count is set to 10, so the message prints 10 times if no argument is given.3fill in blank
hardFix the error in the function by correctly setting the default value for $path to C:\Temp.
PowerShell
function Show-Path {
param(
[string]$path = [1]
)
Write-Output "Path is: $path"
}
Show-Path
Show-Path -path "C:\Windows" Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the string.
Using single quotes which do not interpret escape sequences.
Not escaping backslashes.
✗ Incorrect
The default string value must be enclosed in double quotes with escaped backslashes for the path.
4fill in blank
hardFill both blanks to set default values for $user and $age parameters.
PowerShell
function User-Info {
param(
[string]$user = [1],
[int]$age = [2]
)
Write-Output "User: $user, Age: $age"
}
User-Info
User-Info -user "Bob" -age 25 Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up which default value goes to which parameter.
Using quotes around integer default values.
✗ Incorrect
The default user is set to "Anonymous" and the default age is set to 30.
5fill in blank
hardFill all three blanks to create a function with default values for $file, $mode, and $verbose.
PowerShell
function Process-File {
param(
[string]$file = [1],
[string]$mode = [2],
[bool]$verbose = [3]
)
Write-Output "File: $file, Mode: $mode, Verbose: $verbose"
}
Process-File
Process-File -file "data.txt" -mode "read" -verbose $true Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around boolean values.
Mixing up mode default values.
✗ Incorrect
The default file is "default.txt", mode is "read", and verbose is false by default.