DevOps | Scripts | Automation

Powershell

Function in PowerShell

Like any other programming language, the definition of the function remains same. Function contains a statement, code, loops conditions, etc.

Why to use function in PowerShell?

Function is a great source to reduce the complexity of the program by diving each section of the code into multiple functions according to their functionality.

For example, If you have a script to build the server on Azure Cloud, you can simply divide the script into multiple functions such as a function to check the Azure Connection, a Function to assign resources, a function to build the network, etc.

By doing this you are reducing the great complexity of the program and also avoiding code redundancy by just calling the function and passing the multiple parameters.

In addition, you can validate the parameter passed, make the parameters mandatory in order to work your function and that is useful to avoid writing more code.

Function Definition:

function FunctionName{
    param (
        Parameter 1
        Parameter 2
        ........
        Parameter N
    )
    
    #Statement 1
    #Statement 2
    ...........
    ...........
    #Statement N
}

To call the function, you need to use the name of the function.

Function Types:

There are two types of functions.

  • Simple function
  • Advance Function

The above definition we have seen was a simple function and the advance function is also similar in declaring and calling but it supports more validation and cmdlet bindings. We will see it in the next upcoming article.

Simple Function Example:

function PrintText{
    Write-Output "Hello PowerShell"
}

To use this function, we need to call it with the name. Here, the function name is PrintText. But before that, you first need to load the program into memory by executing it (F5)

You can also call this function inside the script by mentioning its name. For example,

Passing Arguments to Function:

For the function or code reusability, you can pass the arguments to the function and function captures it by parameters. In the above example, let say we need to print the different string every time we call the function so we pass the arguments to the function.

In the above example, “Hello PowerShell” string is captured in $str parameter.

If we have multiple arguments to send, we can separate them by space.

For the function reusability you can use the above program and instead of hardcoding the values, you can use Read-Host command to get the value and then pass them to functions. For example,

Code:

function PrintText($str1,$str2){
    Write-Output "My Name is $str1 and my profession is $Str2"
}

$name = Read-Host "Enter Name "
$prof = Read-Host "Enter Profession "

PrintText $name $prof

For more advance technique, we can use the param property inside the function to pass the value and that is covered in the next article.

Return Output from Function:

PowerShell function can also return the output out of the function. In the below example, we have function name Multi which multiplies two numbers and returns the multiplication output.

You can also store this output for the future use. For example,

In the above example, First, the function name “Multi” called by passing two values which multiplies them and sends results to the $out variable to store. The second function “Add20” takes the Output as the input and adds 20 to the value and delivers the result.

Code:

function Multi($val1,$val2){
   return $val1 * $val2
}

Function Add20($out){
    Write-Output "Final Output : $($out + 20)"
} 

#Calling First Function
$out = Multi 5 5

#Calling Second Function
Add20 $out