A function is a named block of code which is used to define reusable code that should be easy to use. It is usually included inside a script to help reuse code (to avoid duplicate code) or distributed as part of a module to make it useful for others in multiple scripts.
Scenarios where a function might be useful:
c$
-shareFunctions are created using the function
keyword, followed by a single-word name and a script block containing the code to executed when the function name is called.
function NameOfFunction {
Your code
}
function HelloWorld {
Write-Host "Greetings from PowerShell!"
}
Usage:
> HelloWorld
Greetings from PowerShell!