0
0
PowerShellscripting~10 mins

Why functions organize scripts in PowerShell - Test Your Understanding

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

Complete the code to define a function named 'Greet' that prints a message.

PowerShell
function [1] {
    Write-Output "Hello, friend!"
}
Drag options to blanks, or click blank then click option'
AGreet
BSayHello
CPrintMessage
DHelloWorld
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than 'Greet'.
Forgetting to write 'function' keyword.
2fill in blank
medium

Complete the code to call the function named 'Greet'.

PowerShell
[1]
Drag options to blanks, or click blank then click option'
AGreet
BInvoke-Greet
Cfunction Greet()
DWrite-Output 'Greet'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call the function with 'function' keyword.
Using Write-Output instead of calling the function.
3fill in blank
hard

Fix the error in the function definition by completing the blank.

PowerShell
function Greet {
    [1] "Hello from function!"
}
Drag options to blanks, or click blank then click option'
APrint
BWrite-Output
CSay
Decho
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Print' or 'Say' which are not PowerShell commands.
Using 'echo' which works in some shells but not recommended here.
4fill in blank
hard

Complete the code to create a function 'Add' that takes two numbers and returns their sum.

PowerShell
function Add($a, $b) {
    return $a [1] $b
}
$result = Add 5,3
Write-Output $result
Drag options to blanks, or click blank then click option'
A+
B,
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' or '*' instead of '+'.
Not separating arguments with a comma.
5fill in blank
hard

Fill all three blanks to define a function 'IsEven' that returns true if a number is even.

PowerShell
function IsEven($num) {
    if ($num [1] 2 [2] 0) {
        return [3]
    } else {
        return $false
    }
}
Write-Output (IsEven 4)
Drag options to blanks, or click blank then click option'
A%
B-eq
C$true
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '-eq' for equality.
Returning 'true' instead of '$true'.