EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Functions

PowerShell Functions

Chirag Nagarekar
Article byChirag Nagarekar
Priya Pedamkar
Reviewed byPriya Pedamkar

Updated February 28, 2023

PowerShell Functions

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

PowerShell Functions Example

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:

PowerShell Functions Example

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:

PowerShell Functions Example 1

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:

One Argument Example 2

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:

Multiple Arguments Example 3

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:

PowerShell Functions Example 4

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:

PowerShell Functions Example 5

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:

Array Example 6

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:

PowerShell Functions Example 7

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:

Parameter Declaration Example 8

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 –

  1. Switch Case Works in PowerShell?
  2. PowerShell Tools
  3. PowerShell Scheduled Task
  4. PowerShell Convert to String
  5. PowerShell Send Mail
ADVERTISEMENT
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
  • Blog as Guest
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Cyber Monday Reloaded Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW