Adding Parameters to PowerShell Function

PowerShell Function Parameters: How to Add?


When developing your PowerShell functions, sometimes you need to put some input values to your functions, such as a file name, string, or any other value. In PowerShell, there are two ways to pass parameters to functions: through the $Args variable and by setting formal parameters.

Passing Parameters to the PoSh Function Through the $Args Variable

Let’s look at the simplest PowerShell function example that displays some text on screen:

Function TestPoShArgs { 

“I love PowerShell”

}

Add the $Args variable to the function:

Function TestPoShArgs { 

“I love PowerShell $Args”

}

And run it with a few parameters:

TestPoShArgs One Two Three

powershell function parameters

In this example, we passed 3 parameters to the function input, separated by spaces. At the output of the function, they also appear with spaces. You can change the output delimiter using the special variable $OFS.

Function TestPoShArgs { 

$OFS=","

“I love PowerShell $Args”

}

TestPoShArgs One Two Three

Now the returned values in the function results are separated by commas.

powershell function parameter

The $Args variable is a simple PowerShell array and you can access its individual values by sequence numbers (starting from 0).

Function TestPoShArgs { 

"Total args number: $($Args.Count)" 

For($i=0; $i -lt $Args.Count; $i++) {$Args[$i]}

}

TestPoShArgs One Two Three Four

As you can see, the function calculated the number of arguments that were passed to it and listed them line by line.

powershell function param

Passing Formal Parameters to PowerShell Functions

As in other programming languages, you can set a number of formal parameters for PowerShell functions. During the function execution, the values of these parameters will be replaced by the arguments passed by user.

Formal parameters are passed in parentheses after the function name:

Function TestPoShParam ($curwidth, $curheight) { 

 $summ=$curwidth+$curheight

 “Current summ $summ"

}

TestPoShParam 800 600

This function accepts two parameters as input and displays their sum.

powershell function arguments

When specifying function arguments, you can specify parameter names; in this case, their order doesn’t matter:

TestPoShParam -curheight 800 -curwidth 800

By default, PowerShell itself tries to determine the type of value in the parameter. But you can specify the type of the variable manually. For example:

Function TestPoShParam ([int] $curheight_meter, [string] $curname) {

“The mountain " + $curname + " is about " + ($curheight_meter/1000) +" km”

}

TestPoShParam 8848 'Everest' 

powershell function default parameter

If you swap parameters with the given types, Powershell functions will return an error:

TestPoShParam : Cannot process argument transformation on parameter ‘curheight_meter’. Cannot convert value

“Everest” to type “System.Int32”. Error: “Input string was not in a correct format.”

Also in the PowerShell function, you can specify that some parameters are mandatory required and their order. The following syntax is used for this:

Function TestPoShParam { 

PARAM (

 [PARAMETER(Mandatory=$True,Position=0,HelpMessage ="Specify full DB path")][ValidateLength(1,20)][String]$DBPath,

 [PARAMETER(Mandatory=$True,Position=1,HelpMessage ="Specify max DB size")][String]$MaxSize,

 [PARAMETER(Mandatory=$False,Position=2)][String]$Type='MSSQL'

)

 Return("Path:" +$DBPath + " DB_size:" + $maxsize + " DB_type:" + $Type) 




}

TestPoShParam -DBPath 'c:\mysql\' -MaxSize 20

powershell function parameter types

In this example, additional arguments appeared near the Parameter switch:

  • Mandatory – if True is specified, this function parameter is strongly required;
  • HelpMessage – help for the user on the function parameter;
  • Position – position of the argument when calling the function (Parameters must be specified in a strictly defined order. If you don`t specify one of the parameters, then PowerShell will automatically remind you of this and offer to enter the missing information);
  • ValidateLength – maximum argument length (you cannot use a value with a length that is greater than the specified argument limit).

You may also like:

How to transfer FSMO Roles From a Failed Domain Co... In case domain controller, which owns FSMO (Flexible Single Master Operation) roles, is fail (virus attack, fatal software problems or catastrophic ha...
Time Configuration for a Virtualized Domain Contro... Today we will talk about some of the features of time configuration on a virtualized domain controllers. Typically, the time synchronization scheme in...
Removing Old and Unused Drivers from Driver Store ... Each time you install or update your device driver, Windows OS (since Vista) continues to store the old version of the driver in the system Driver Sto...
Get-service: Checking the Status of Windows Servic... Using the Get-Service cmdlet you can get a list of all the services installed on the operating system, its status and startup type. This one and other...
How to Create a GUI for PowerShell Scripts? One of the significant drawbacks of PowerShell scripts, when used by users (not sysadmins or programmers), is its command-line interface. The result o...

Add Your Comment