Function
suggest changeA 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:
- Calculate the average of a group of numbers
- Generate a report for running processes
- Write a function that tests is a computer is “healthy” by pinging the computer and accessing the
c$
-share
Functions 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
}
Demo
function HelloWorld {
Write-Host "Greetings from PowerShell!"
}
Usage:
> HelloWorld
Greetings from PowerShell!
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents