Functions in PowerShell

By Jul 11, 2018

Description:

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

Preferencesoft

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
FunctionName

Functions without parameters

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

Func1.ps1

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

# Calling the function
Write-Date

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

Set-ExecutionPolicy Unrestricted

Output

2018/07/09 10:30:36

We can give a lite version of the previous function:

Func2.ps1

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

Echo-Date

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.

Func3.ps1

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


Union -s1 "abcd" -s2 "123456789"

Output

abcd-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 {
Param([string]$str)
    $n=$str.Length
    If ($n -gt 0) {
        Return $str[$n-1]
    }
    Else {
        Return ""
    }
}

# Calling the function
Last-Char "a1a2a3a4a5"

Output

5

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

Output

14

 


PowerShell

Categories

Share

Follow


KodFor Privacy Policy