PowerTip: Use positional parameters


Summary: Cloud & Datacenter Management MVP, Thomas Rayner, shows how to use positional parameters in Windows PowerShell.

Hey, Scripting Guy! Question I always see people pass variables or data to a function without specifying the parameter name that the data is for, for example:

copy-item $source $destination” acts like “copy-item –path $source –destination $destination

How do I do that in my functions, and how does the function know which variable is the path and destination?

Hey, Scripting Guy! Answer In functions, you declare parameters before doing much else. When declaring your parameter, indicate the Position starting with 0, for example:

Function Invoke-Test

{

[CmdletBinding()]

Param(

[Parameter(Position = 0)]

[string]$First,

[Parameter(Position = 1)]

[string]$Second

)

Comments (0)

Skip to main content