0
0
PowerShellscripting~5 mins

Function definition in PowerShell - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a function in PowerShell?
A function is a named block of code that performs a specific task and can be reused multiple times in a script.
Click to reveal answer
beginner
How do you define a simple function named SayHello in PowerShell?
Use the syntax:<br>
function SayHello {
  Write-Output "Hello!"
}
Click to reveal answer
beginner
What keyword starts a function definition in PowerShell?
The keyword function is used to start defining a function.
Click to reveal answer
beginner
How can you call or run a function named SayHello after defining it?
Simply type the function name:<br>
SayHello
This runs the code inside the function.
Click to reveal answer
beginner
Why use functions in scripts?
Functions help organize code, avoid repetition, and make scripts easier to read and maintain.
Click to reveal answer
Which keyword is used to define a function in PowerShell?
Adef
Bfunction
Cfunc
Dsub
How do you call a function named GetDate in PowerShell?
Acall GetDate()
Brun GetDate
CGetDate
Dexecute GetDate()
What does the following function do?<br>
function Greet { Write-Output "Hi!" }
APrints 'Hi!' when called
BDefines a variable named Greet
CDeletes the string 'Hi!'
DCreates a file named Greet
Why should you use functions in your scripts?
ATo repeat code multiple times
BTo slow down the script
CTo make the script longer
DTo organize and reuse code easily
Which of these is a correct function definition in PowerShell?
Afunction Hello { Write-Output "Hello" }
Bdef Hello() { echo "Hello" }
Cfunc Hello() { print "Hello" }
Dsub Hello { Write "Hello" }
Explain how to define and call a simple function in PowerShell.
Think about the keywords and how you run the function after defining it.
You got /4 concepts.
    Describe why functions are useful in scripting with PowerShell.
    Consider how functions help when scripts get bigger.
    You got /4 concepts.