February 16th, 2016

PowerTip: Use positional parameters

Doctor Scripto
Scripter

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

)

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.

Feedback