Functions in PowerShell

By Jul 11, 2018


Here we explain how to declare a function in PowerShell with or without parameters and give several examples.


A function in PowerShell is a named block of statements, possibly including parameters and which is called from any place by its name (possibly followed by values)

Function FunctionName {
    # Statements

# Calling the function

Functions without parameters

Here is an example of a function without parameters that displays the current date in a certain format:


Function Write-Date {
    Write-Output (Get-Date -format 'yyyy/MM/dd HH:mm:ss')

# Calling the function

Before you run this function, you will need to run Ise or PowerShell and change the execution Policy.

Set-ExecutionPolicy Unrestricted


2018/07/09 10:30:36

We can give a lite version of the previous function:


Function Echo-Date {
    Get-Date -format 'yyyy/MM/dd HH:mm:ss'


Note that we have omitted Write-Output (aka echo) since the result will be returned by Get-Date on the standard output.

Functions with parameters

To declare a function with parameters, we put inside the block of the function the keyword Param followed by the list of parameters in parentheses.

For example, declare the Union function that joins two strings with a hyphen between the two.


Function Union {
Param ([string] $s1, [string] $s2)
    Write-Host ($s1 + "-" + $s2)

Union -s1 "abcd" -s2 "123456789"



The function can be called as follows:

Union -s1 "abcd" -s2 "123456789"

Or without specifying the names of the parameters:

Union "abcd" "123456789"

In the latter case, the values are transmitted to the function in the order of placement. It is therefore generally unnecessary to specify parameter names unless you want to change their order:

Union -s2 "123456789" -s1 "abcd" 

Parentheses and commas

A common mistake for beginners in PowerShell is to call the functions by placing the values in parentheses and separated by commas, as follows:

Union ("abcd", "123456789")

But in PowerShell parentheses are used to group objects and here ("abcd", "123456789") will be interpreted as the string "abcd 123456789". The missing parameter will be replaced by the empty string.

On the other hand when we call a method of any class, we place the arguments in parentheses and separated by commas:

[string]$str = "1.2345"
[int]$num = [convert]::ToInt32($str, 10)

Functions returning a value

For a function, there are several ways to return a result:

  • By standard output (with Write-Output, echo or any function leaving a result on the standard output),
  • With the Return statement

Let's give an example of a function that from a string returns the last character.

Function Last-Char {
    If ($n -gt 0) {
        Return $str[$n-1]
    Else {
        Return ""

# Calling the function
Last-Char "a1a2a3a4a5"



Here is an example of a function that from 3 integers returns the sum of the squares of these integers.

Function SquareSum3 {
Param([int] $i1, [int] $i2, [int] $i3)
    Return $i1*$i1 + $i2*$i2 + $i3*$i3

SquareSum3 1 2 3