Windows Vista Forums

$args
  1. #1


    IT Staff Guest

    $args

    I am passing $args to my script.

    I want to know the $args syntax that can take in "any" number of arguments.

    If i put $args[1], it means 2 arguments.

    I want to take in *any* number of arguments ..what is the syntax ? Is it
    $args[] or $args() ?





      My System SpecsSystem Spec

  2. #2


    Jeff Guest

    Re: $args

    On Jan 18, 10:44 am, "IT Staff" <jkk...@xxxxxx> wrote:

    > I am passing $args to my script.
    >
    > I want to know the $args syntax that can take in "any" number of arguments.
    >
    > If i put $args[1], it means 2 arguments.
    >
    > I want to take in *any* number of arguments ..what is the syntax ? Is it
    > $args[] or $args() ?
    Once inside a script, all of the parameters passed to the script are
    contained in $args, whatever the number. You access each one using
    array subscripting, like you have shown above:

    # argument one
    $arg[ 0 ]

    #argument two
    $arg[ 1 ]

    If you use a param statement at the top of your script (which I highly
    recommend, as it makes scripts much easier to maintain), $args holds
    any additional arguments.

    # test-param.ps1
    param( $first, $second )

    $first
    $second
    for ( $i = 0; $i -lt $args.count; $i++ )
    {
    $args[ $i ]
    }
    # test-param.ps1

    PS$ .\test-param.ps1 "first" "second" "third" "fourth"
    first
    second
    third
    fourth

    I hope this clears things up a bit.

    Jeff

      My System SpecsSystem Spec

  3. #3


    Jacob Saaby Nielsen Guest

    Re: $args

    Hey Jeff,

    > # argument one
    > $arg[ 0 ]
    > #argument two
    > $arg[ 1 ]
    Shouldn't that be $args[0] and $args[1] ?

    Anyway, IT Staff, try this code in a script to get an idea of how $args work,
    save it to argstest.ps1:

    -----------
    for ($i=0; $i -lt $args.length; $i++)
    { Write-Host 'This is $args'[$i]':' $args[$i] }

    Write-Host "`n"'And this is outputting $args by itself'":`n"
    $args
    -----------

    Now run it like this, in PowerShell: .\argstest.ps1 one two three four five
    six seven eight nine ten

    As long as you use $args, you can use any number of arguments. Because $args
    is an array that takes in
    any number of arguments. Feed it 1, it has 1 element. Feed it 1000, it has
    1000 elements.

    And to answer your other question, because it is an array, you use the square
    brackets [] after the array-
    name. Or simply $args if you want to output the whole array.

    However, like Jeff, I'd recomment using the Param functionality, something
    I only just began to do myself.

    Best Regards,
    Jacob Saaby Nielsen

    http://www.pipforhelvede.net
    gmail: jacob DOT saaby
    hotmail (IM): same as gmail

    > On Jan 18, 10:44 am, "IT Staff" <jkk...@xxxxxx> wrote:
    >

    >> I am passing $args to my script.
    >>
    >> I want to know the $args syntax that can take in "any" number of
    >> arguments.
    >>
    >> If i put $args[1], it means 2 arguments.
    >>
    >> I want to take in *any* number of arguments ..what is the syntax ? Is
    >> it $args[] or $args() ?
    >>
    > Once inside a script, all of the parameters passed to the script are
    > contained in $args, whatever the number. You access each one using
    > array subscripting, like you have shown above:
    >
    > # argument one
    > $arg[ 0 ]
    > #argument two
    > $arg[ 1 ]
    > If you use a param statement at the top of your script (which I highly
    > recommend, as it makes scripts much easier to maintain), $args holds
    > any additional arguments.
    >
    > # test-param.ps1
    > param( $first, $second )
    > $first
    > $second
    > for ( $i = 0; $i -lt $args.count; $i++ )
    > {
    > $args[ $i ]
    > }
    > # test-param.ps1
    >
    > PS$ .\test-param.ps1 "first" "second" "third" "fourth"
    > first
    > second
    > third
    > fourth
    > I hope this clears things up a bit.
    >
    > Jeff
    >


      My System SpecsSystem Spec

  4. #4


    cashfoley Guest

    Re: $args

    On Jan 17, 9:44*pm, "IT Staff" <jkk...@xxxxxx> wrote:

    > I am passing $args to my script.
    >
    > I want to know the $args syntax that can take in "any" number of arguments..
    >
    > If i put $args[1], it means 2 arguments.
    >
    > I want to take in *any* number of arguments ..what is the syntax ? Is it
    > $args[] or $args() ?
    If I read your question correctly, I'm working on a similiar problem
    causing me to make a post.

    http://groups.google.com/group/micro...af8c8c7c79a401

    $script = [scriptBlock] { $x = $Args[0]; $y = $Args[1]; Write-Host "X:
    $x Y:$y"; $x+$y}
    $parms = 4,5
    $script.InvokeReturnAsIs($parms)

    In this example, $parms can have any number of entries and accessed
    through the $Args



      My System SpecsSystem Spec

  5. #5


    cashfoley Guest

    Re: $args

    On Jan 18, 8:56*am, cashfo...@xxxxxx wrote:

    >
    > If I read your question correctly, I'm working on a similiar problem
    > causing me to make a post.
    >
    > http://groups.google.com/group/micro....powershell/br...
    >
    > $script = [scriptBlock] { $x = $Args[0]; $y = $Args[1]; Write-Host "X:
    > $x Y:$y"; $x+$y}
    > $parms = 4,5
    > $script.InvokeReturnAsIs($parms)
    >
    > In this example, $parms can have any number of entries and accessed
    > through the $Args
    If the script is an existing function it can be invoked as in this
    example:

    function test
    {
    $x = $Args[0]
    $y = $Args[1]
    Write-Host "X: $x Y:$y"
    $x+$y
    }

    $testdef = Get-ChildItem function:test
    $parms = 4,5
    $testdef.ScriptBlock.InvokeReturnAsIs($parms)

      My System SpecsSystem Spec

  6. #6


    Keith Hill [MVP] Guest

    Re: $args

    "Jeff" <jeff.hillman@xxxxxx> wrote in message
    news:911677a6-c95f-4e03-a64d-899f9933fdb5@xxxxxx

    > Once inside a script, all of the parameters passed to the script are
    > contained in $args, whatever the number.
    Except for any arguments that have been bound to defined parameters
    (function($a,$b) or use of param($a,$b)). In this regard, PowerShell $args
    behaves like Rest parameters in Lisp.

    --
    Keith




      My System SpecsSystem Spec

  7. #7


    Jeff Guest

    Re: $args

    On Jan 19, 7:27 am, "Keith Hill [MVP]"
    <r_keith_h...@xxxxxx_no_spam_I> wrote:

    > "Jeff" <jeff.hill...@xxxxxx> wrote in message
    >
    > news:911677a6-c95f-4e03-a64d-899f9933fdb5@xxxxxx
    >

    > > Once inside a script, all of the parameters passed to the script are
    > > contained in $args, whatever the number.
    >
    > Except for any arguments that have been bound to defined parameters
    > (function($a,$b) or use of param($a,$b)). In this regard, PowerShell $args
    > behaves like Rest parameters in Lisp.
    >
    > --
    > Keith
    Keith,

    This is what I meant to point out with my second example with the
    param statement, but I guess I chose my words poorly. Thanks for
    clearing it up.

    Jeff

      My System SpecsSystem Spec

  8. #8


    Keith Hill [MVP] Guest

    Re: $args

    "Jeff" <jeff.hillman@xxxxxx> wrote in message
    news:a46e0f04-5c2f-42e5-8497-16837e278dca@xxxxxx

    > On Jan 19, 7:27 am, "Keith Hill [MVP]"
    > <r_keith_h...@xxxxxx_no_spam_I> wrote:

    >> "Jeff" <jeff.hill...@xxxxxx> wrote in message
    >>
    >> news:911677a6-c95f-4e03-a64d-899f9933fdb5@xxxxxx
    >>

    >> > Once inside a script, all of the parameters passed to the script are
    >> > contained in $args, whatever the number.
    >>
    >> Except for any arguments that have been bound to defined parameters
    >> (function($a,$b) or use of param($a,$b)). In this regard, PowerShell
    >> $args
    >> behaves like Rest parameters in Lisp.
    >>
    >> --
    >> Keith
    >
    > Keith,
    >
    > This is what I meant to point out with my second example with the
    > param statement, but I guess I chose my words poorly. Thanks for
    > clearing it up.
    Oops. Indeed you did point that out. Sorry 'bout that. :-)

    --
    Keith


      My System SpecsSystem Spec

$args problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
What's wrong with args[0] Curious .NET General 5 27 May 2009
@args isn't working here RickB PowerShell 3 18 Sep 2008
Is this a bug with @args RickB PowerShell 4 09 Apr 2008
Help with $Args in script David Kriz PowerShell 2 12 Feb 2008
$args.count IT Staff PowerShell 0 27 Dec 2007