$args are only available for the unbound parameters you pass into your script. Once you start binding params, each param you pass in to your script will be bound based on its value, type or position. Anything not matching your param binding will end up in $args
Let's say I have a script like this
#$args.ps1
write-host "args value 1 : $($args[0]) "
write-host "args value 2 : $($args[1]) "
When I call it, I can index into $args and pull the values out.
& C:\temp\args.ps1 'SomeValue1' 'SomeValue2'
args value 1 : SomeValue1
args value 2 : SomeValue2
However, once I bind params, $args will then only contains values which don't match your bindings. Notice that only the third param I pass in below ends up in $args
param($param1, $param2)
write-host "params value 1 : $($param1) "
write-host "params value 2 : $($param2) "
write-host "args value 1 : $($args[0]) "
write-host "args value 2 : $($args[1]) "
Now you'll some interesting behavior when I pass in additional params here.
& C:\temp\args.ps1 'SomeValue1' 'SomeValue2' 'SomeValue3'
params value 1 : SomeValue1
params value 2 : SomeValue2
args value 1 : SomeValue3
args value 2 :
why is it this way?
Think about it like this: as we can see in the help under get-help about_automatic_variables, $args is provided to map the undeclared parameters passed into a script. The second you begin declaring parameters, you are specifically describing which params your script will use, and $args becomes a bit weird in usage.
$Args
Contains an array of the undeclared parameters and/or parameter
values that are passed to a function, script, or script block.
When you create a function, you can declare the parameters by using the
param keyword or by adding a comma-separated list of parameters in
parentheses after the function name.
*thanks @LotPings for the correction on the behavior of $args, I originally posted in error that $args contians nothing if you bind params.