PowerShell PowerShell Functions Simple Function with No Parameters


Example

This is an example of a function which returns a string. In the example, the function is called in a statement assigning a value to a variable. The value in this case is the return value of the function.

function Get-Greeting{
    "Hello World"
}

# Invoking the function
$greeting = Get-Greeting

# demonstrate output
$greeting
Get-Greeting

function declares the following code to be a function.

Get-Greeting is the name of the function. Any time that function needs to be used in the script, the function can be called by means of invoking it by name.

{ ... } is the script block that is executed by the function.

If the above code is executed in the ISE, the results would be something like:

Hello World
Hello World