fbpx Access Arguments of a Script, Function, or Script Block in Windows PowerShell | Windows PowerShell, Windows Server | HostingUltraso.com
24/7/365 Support

Access Arguments of a Script, Function, or Script Block in Windows PowerShell

Problem

You want to access the arguments provided to a script, function, or script block.

Solution

To access arguments by name, use a param statement:

param($firstNamedArgument, [int] $secondNamedArgument = 0)

"First named argument is: $firstNamedArgument" "Second named argument is: $secondNamedArgument"

To access unnamed arguments by position, use the $args array:

"First positional argument is: " + $args[0] "Second positional argument is: " + $args[1]

You can use these techniques in exactly the same way with scripts, functions, and script blocks, as illustrated by Example 106.

Example 106. Working with arguments in scripts, functions, and script blocks

############################################################################## ## GetArguments.ps1 ## ## Use commandline arguments ############################################################################## param($firstNamedArgument, [int] $secondNamedArgument = 0)

## Display the arguments by name "First named argument is: $firstNamedArgument" "Second named argument is: $secondNamedArgument"

function GetArgumentsFunction { ## We could use a param statement here, as well ## param($firstNamedArgument, [int] $secondNamedArgument = 0)

## Display the arguments by position "First positional function argument is: " + $args[0] "Second positional function argument is: " + $args[1] }

GetArgumentsFunction One Two

$scriptBlock = { param($firstNamedArgument, [int] $secondNamedArgument = 0)

## We could use $args here, as well "First named scriptblock argument is: $firstNamedArgument" "Second named scriptblock argument is: $secondNamedArgument"

}

& $scriptBlock First One Second 4.5

Example 106 produces the following output:

PS >GetArguments First 2 First named argument is: First Second named argument is: 2 First positional function argument is: One Second positional function argument is: Two First named scriptblock argument is: One Second named scriptblock argument is: 4

Discussion

Although PowerShell supports both the param keyword and the $args array, you will most commonly want to use the param keyword to define and access script, function, and script block parameters.

In most languages, the most common reason to access parameters through an $argsstyle array is to determine the name of the currently running script. For information about how to do this in PowerShell.

When you use the param keyword to define your parameters, PowerShell provides your script or function with many useful features that allow users to work with your script much like they work with cmdlets:

  • Users need only to specify enough of the parameter name to disambiguate it from other parameters.
  • Users can understand the meaning of your parameters much more clearly.
  • You can specify the type of your parameters, which PowerShell uses to convert input if required.
  • You can specify default values for your parameters.

The $args array is sometimes helpful, however, as a way to deal with all arguments at once. For example:

function Reverse

{ $argsEnd = $args.Length 1 $args[$argsEnd..0]

}

produces

PS >Reverse 1 2 3 4 4 3 2 1

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say