Updated February 28, 2023
Introduction to PowerShell Functions
PowerShell function is used for the code reusability. When you write a big program, many times there are chances that you have to use the codes multiple times, so instead of writing it again and again, we can create a function that has all codes inside and call the function whenever necessary. PowerShell has two types of functions. Normal function and Advanced function.
Syntax:
function [<scope:>]<name> [([type]$parameter1[,[type]$parameter2])]
{
param([type]$parameter1 [,[type]$parameter2])
dynamicparam {<statement list>}
begin {<statement list>}
process {<statement list>}
end {<statement list>}
}
Syntax for calling function:
functionname Argument1 Argument2
Parameters of PowerShell Functions
1. Scope: Scope of the function, used to prevent other objects, variables, functions, alises etc., from being inadvertently changed by scripts within the system.
2. Name: Name of the Function.
3. Parameters: When you pass the argument to the function, these parameter captures the arguments and pass them inside the function. This parameter has the same datatype as the argument.
4. Param: Param is useful for declaring the variables inside the function. It is also used with the advanced function.
5. Dynamicparam: Dynamic parameters are used for the advanced functions. Since only included whenever desired.
How do Functions Work in PowerShell?
The function contains the codes and can call another function(s). To create a function, as mentioned in the above syntax, you first need to create the function and then call it within a program, or you can call the function outside a program once it is executed. For example:
Code:
function Printvalue($value1){
Write-Output "Value is : $value1"
}
Printvalue "Hello World"
Output:
Explanation: Here, we have first declared a function called PrintValue, which prints the data, and we will call the function with the function name and by passing the argument “Hello World” and $Value1 captures it and pass it to the function.
You can also just write the function and execute it by the function name from the PowerShell console. For Example: Take the same above function but don’t print the value inside the function, but we will print it from the console after program execution.
Code:
function Printvalue($value1){
Write-Output "Value is : $value1"
}
Output:
Examples to Implement PowerShell Functions?
Below are the examples of PowerShell Functions:
Example #1
PowerShell “function” with no argument. Below is an example of a function without any arguments and parameters.
Code:
function callfunct{
Write-Output "Function called"
}
callfunct
Output:
Example #2
PowerShell “function” with one argument. An example of passing one argument to function and function captures it with one parameter.
Code:
function callfunct($param1){
Write-Output "Value : $param1"
}
callfunct 20
Output:
Example #3
PowerShell “functions” with multiple arguments. You can pass multiple arguments to a function.
Code:
function addition($value1, $value2){
$value = $value1 + $value2
Write-Output "Total : $Value"
}
addition 20 30
Output:
Similarly, you can pass more arguments to the function. For example,
Functionname Argument1 Argument2 Argument3…… ArgumentN
Example #4
PowerShell “function” with the “Return” value. You can use the return parameter to return the output from the function to the call.
Code:
function addition($value1, $value2){
return $value1 + $value2
}
$output = addition 20 30
Write-Output "Sum : $output"
Output:
Here, the function returns the value, and it is captured by the $output variable.
Example #5
Passing an array to the function. We can also pass the entire array to the function.
Code:
$a = @(10,20,30,40,50)
function arrayfunct($values){
Write-Output $values
}
arrayfunct $a
Output:
Example #6
Return array from the function. An array can be returned too from the function. See the example below.
Code:
function callarray{
$a = @(10,20,30,40,50)
return $a
}
$arr = callarray
Write-Output $arr
Output:
In the example above, the function returns the array values and which are captured by variable $arr.
Example #7
Nested functions. You can call the function inside the function as well. In the below example, we will pass two values to a function, which will be summed up and then passed to another function to get double its value.
Code:
function square($a){
return $a * $a
}
function addition($value1,$value2){
$sum = $value1 + $value2
$sqr = square $sum
return $sqr
}
$out = addition 5 6
Write-Output "Square : $out"
Here, two values, 5 and 6, are passed to function addition, which will be summed up and then the output will be passed to the square function (value: 11) to get the square of it and finally, the output will be returned is 121.
Output:
Example #8
Parameter declaration inside the PowerShell Function. You can declare parameters inside the function. So at the beginning of the function, parameters will be initialized. It is similar to assigning the values to the variables but looks more structured when you use the advanced function.
Code:
function parametertest{
param(
$name = "Donald",
$Citizen = "US"
)
Write-Output "Name : $name"
Write-Output "Citizen : $citizen"
}
parametertest
Output:
Conclusion
There is no doubt that function provides the great reusability of the code with advanced functionality. The function is also an important part of any programming language because it creates a program in a more structural and easy to understand the flow of the program. Most people use the advanced function while writing the huge lines of codes, so it also helps in capturing the error output and for the debug purpose.
Recommended Articles
This is a guide to PowerShell Functions. Here we discuss the Introduction to the PowerShell Functions and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –