How to pass multiple parameters into a function in PowerShell – In this post, I will show how to pass multiple parameters into a function in PowerShell coding.
Addition of numbers function using PowerShell example: Function with multiple parameters PowerShell
cls #The below sample function will accept numeric parameters and do the addtion. Function Add-Numbers() { param ( [Parameter(Mandatory=$true)] [int32] $FirstNumber, [Parameter(Mandatory=$true)] [int32] $SecondNumber, [Parameter(Mandatory=$false)][int32] $ThirdNumber ) Try { $resultNumber=$FirstNumber+$SecondNumber+$ThirdNumber; Write-host -f Green "Addition of" $FirstNumber, $SecondNumber "and" $ThirdNumber "is: " $resultNumber } Catch { $ErrorMessage = $_.Exception.Message +"while doing the addition" Write-Host $ErrorMessage -BackgroundColor Red } return $resultNumber; } #Parameters - how to pass parameter to the function: $firstNumber=10; $secondNumber=10; $thirdNumber=10; Add-Numbers $firstNumber $secondNumber $thirdNumber #Parameters ends
Test the addition of numbers function script: Function with multiple parameters PowerShell
Summary: Pass multiple parameters into a function in PowerShell(Function with multiple parameters PowerShell)
Hence, in this post we have learned the below concepts of PowerShell:
- How to make a parameter mandatory in PowerShell
- How to make a parameter optional in PowerShell
- What are the parameters in PowerShell?
- How to add multiple parameter sets to a PowerShell function
- How to use Parameters in PowerShell
- Getting input into PowerShell Functions with Parameters
- Number (int32) datatype in PowerShell.
- The parameters,data types,return value in PowerShell function.
See Also: PowerShell tutorial for beginners
You may also like the below PowerShell tutorials:
- Exception or Error handling in PowerShell -Try/Catch/Finally Block Example
- Using PowerShell create a log file
- Using PowerShell – Create a folder if not exists
- Encode and decode an URL using PowerShell coding
- Office 365: Getting started with SharePoint PnP PowerShell – installation
- In 2 steps convert a classic SharePoint page to modern using PnP
- Office 365: Retrieve hub sites and associated sites using PnP Powershell
- Create a modern team site using PnP PowerShell in SharePoint
- In 4 steps access SharePoint online data using postman tool
- SharePoint admin center: Learn SharePoint online administration in an hour – step by step
- SharePoint REST API: GET vs POST vs PUT vs DELETE vs PATCH
- Office 365: Understanding the hub site in SharePoint online
- Introduction to PowerShell
