Powershell

PowerShell Function | A Detailed Guide

The PowerShell function is used when we want to use one piece of code multiple times in a script. A function is a combination of PowerShell statements whose name is set by the user. Whenever we execute any function, we usually type the function name. If you want to reuse the PowerShell commands and scripts in multiple scenarios, then it is possible with PowerShell functions.

In this guide, a detailed guide about PowerShell functions is provided.

What is a Function in a PowerShell?

A function in PowerShell is a collection of statements of code with input and output. It forms the sequence of instructions to execute the code once or more than one time by invoking it. To increase the readability and usability of the code, the functions are used because it makes it easy to deal with the code being repeated.

It is concluded that the purpose of the function is the same in PowerShell as in any other programming language. Functions in PowerShell are categorized into two types that are written below:

  • Simple function
  • A function with parameters (Known as an “Advanced Function” or a “Simple Function with parameters”)

In the upcoming part of the guide, these function types are explained in detail.

PowerShell Simple Function

The PowerShell simple function is the same as the basic function in other well-known programming languages. Whenever you want to create a function, then “Simple Function” is the simplest form of function.

Syntax

function  <function-name>
{
code-statement-1
code-statement-2
}

This function does not include any built-in features, the syntax of the PowerShell simple function is described below:

  • The function keyword is used to initialize a function followed by its name.
  • The body of the function resides inside the pair of curly braces.

Let’s experience it with the upcoming example.

Example: How to Create a Simple Function in PowerShell

Open PowerShell ISE and navigate to the scripting pane to write the following code. The example code creates a simple function that contains an “echo” statement inside it.

function print
 {  
 echo "Welcome to LinuxHint"    
 }
Write-Host "The content of the function is: "
print

Execute/run the script by providing the absolute path of the script:

The output shows that the content of the function is printed on the console.

Note: Learn How a PowerShell script can be run.

PowerShell Advanced function

The advanced function is almost similar to the simple function because both share similar properties. However, the advanced function includes some extra features like the parameters support. These are the most important streams for displaying output accurately.

An advanced function in PowerShell follows the below-mentioned syntax:

Syntax

Function Verb-Noun {
CmdletBinding[]
param ()
begin {}
process {}
end {}
}

The instances of the above syntax are:

  • Function is the keyword used to create a function.
  • Verb-Noun represents the name of the function where Verb and Noun are used from a predefined set from PowerShell’s library.
  • The param() part contains the parameters declared by the user.
  • In the begin{} part, the values of variables are initialized.
  • The process{} executes the variables and parameters initialized in the param() and begin{} parts, respectively.
  • Lastly, the end{} segment appears at last and clears the variables/parameters.

Note: Among the above stated components, the begin{}, process{}, and end{} portions are optional.

Example: How to Create an Advanced Function in PowerShell

In the following lines of code, a PowerShell advanced function is created that adds the two numbers.

Code

function Add-Num {
    param([int]$a , [int]$b)
    Write-Host $a + $b
}
$a= Read-Host "Enter the first number"
$b= Read-Host "Enter the second number"
if(Add-Num $a $b -lt 10) {
Write-Host "Sum of $a and $b is a digit"
 }
 else {
 Write-Host "Sum of $a and $b is a number"
 }

In the above code:

  • A function named Add-Num is initialized.
  • Two integer variables are declared as a parameter.
  • On the last line of the Add-Num function, these variables are added and printed using the Write-Host cmdlet.
  • After that, the Read-Host cmdlet is used to take input from the user.
  • The Add-Num function is then used in the if-else condition.
  • The if-else condition will compare the sum of the numbers, and if the sum is less than 10, then the if block will be executed otherwise else block.

Output

Execute the script by using the absolute path (or you can use the relative path as well):

E:\TSEP\adfunc.ps1

It can be observed from the output that the function (Add-Num) calculates the sum, and then it shows that the returned sum is a number.

Conclusion

The function in PowerShell consists of a list of statements, and it has a specific name assigned to it. It starts with the function keyword followed by a user-defined name, and the function’s body is inside the curly brackets. PowerShell functions have two types, one is a simple function, and the other is an advanced function. In this write-up, you have learned the basics of simple as well as advanced functions in PowerShell.

About the author

Adnan Shabbir