0
0
PowerShellscripting~10 mins

Default parameter values in PowerShell - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A"Visitor"
B"User"
C"Admin"
D"Guest"
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.
2fill in blank
medium

Complete 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'
A10
B5
C3
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around numbers for integer parameters.
Not assigning any default value.
3fill in blank
hard

Fix 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'
AC:\Temp
B'C:\Temp'
C"C:\\Temp"
D"C:/Temp"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the string.
Using single quotes which do not interpret escape sequences.
Not escaping backslashes.
4fill in blank
hard

Fill 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'
A"Anonymous"
B30
C25
D"Guest"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up which default value goes to which parameter.
Using quotes around integer default values.
5fill in blank
hard

Fill 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'
A"default.txt"
B"read"
C$false
D"write"
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around boolean values.
Mixing up mode default values.