PowerShell - Getting Started with functions

by
Jeremy Canfield |
Updated: May 16 2023
| PowerShell articles
A function is a collection of reusable markup. In this example, there is a subroutine named "foo" that will print "Hello World";
function greeting {
Write-Host "Hello World"
}
Or like this.
function greeting {
$hello = "Hello World"
}
In this example, the following markup will invoke the greeting, which will print "Hello World".
greeting
Often, functions have a return.
function greeting {
return "Hello World"
}
In this example, the string returned in the greeting function is stored in the $hello variable.
$hello = greeting
Write-Host $hello
Often, values are passed into a function.
function greeting($first, $last) {
return "Hello $first $last"
}
In this example, "Hello John Doe" should be printed.
$hello = greeting 'John' 'Doe'
Write-Host $hello
Did you find this article helpful?
If so, consider buying me a coffee over at