38

I have a test powershell V2 script that looks like this:

    function test_args()
    {
      Write-Host "here's arg 0: $args[0]"
      Write-Host "here's arg 1: $args[1]"
    }


    test_args

If I call this from the powershell command prompt I get this on the screen:

here's arg[0]: [0]

here's arg[1]: [1]

Not quite what I wanted. It seems I have to copy $args[0] and $args[1] to new variables in the script before I can use them? If I do that I can access things fine.

Is there a way to access the indexed $args in my code? I've tried using curly braces around them in various ways but no luck.

I'll be moving to named parameters eventually, but the script I'm working on (not this demo one) is a straight port of a batch file.

1
  • 4
    Ultimately, this is string-expansion (interpolation) syntax problem, as also explained in the accepted answer. In short: To embed an expression such as $args[0] - as opposed to a mere variable reference such as $var - you need $(...), the subexpression operator; ergo: "... $($args[0]) ...". For a concise summary of PowerShell's string-expansion rules, see this answer. Commented Nov 15, 2019 at 16:18

1 Answer 1

66

Try this instead:

function test_args()
{
  Write-Host "here's arg 0: $($args[0])"
  Write-Host "here's arg 1: $($args[1])"
}

test_args foo bar

Note that it is $args and not $arg. Also when you use a PowerShell variable in a string, PowerShell only substitutes the variable's value. You can't directly use an expression like $args[0]. However, you can put the expression within a $() sub-expression group inside a double-quoted string to get PowerShell to evaluate the expression and then convert the result to a string.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you Keith-- I changed my snippet to correct my typo when translating from my editor to SO. I guess my question is, do I need to pass $arg[0] and [1] to test_args as parameters, or will the function itself have access to the $args array?
Functions always have access to $args. It will contain any argument that doesn't map to a defined function parameter. If there are no arguments or all the arguments map to function parameters e.g. function foo($param1, $param2){} then $args will be an empty array.
Sometimes I have to use Out-String to get the output of a sub-expression to display as desired ... such as when working with a hashtable. Example, assumes arg 1 is a hashtable: Write-Host "here's arg 1: $($args[1] | Out-String)"
Small caveat re $args availability: Advanced functions (or scripts) - namely those whose param() block is decorated with the [CmdletBinding()] attribute and/or whose parameters are decorated with [Parameter()] attributes - fundamentally do not permit invocation unless all arguments passed bind to declared parameters. In other words: $args does not apply to advanced functions and scripts.
I use $args in function n{notepad $args} and call it with n argg_file. This seems PS kungfu?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.