Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

$args

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 01-17-2008   #1 (permalink)
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
Old 01-18-2008   #2 (permalink)
Jeff
Guest


 

Re: $args

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

> 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
Old 01-18-2008   #3 (permalink)
Jacob Saaby Nielsen
Guest


 

Re: $args

Hey Jeff,
Quote:

> # 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
Quote:

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

>> 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
Old 01-18-2008   #4 (permalink)
cashfoley
Guest


 

Re: $args

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

> 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
Old 01-18-2008   #5 (permalink)
cashfoley
Guest


 

Re: $args

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

>
> 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
Old 01-18-2008   #6 (permalink)
Keith Hill [MVP]
Guest


 

Re: $args

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

> 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
Old 01-18-2008   #7 (permalink)
Jeff
Guest


 

Re: $args

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

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

> > 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
Old 01-19-2008   #8 (permalink)
Keith Hill [MVP]
Guest


 

Re: $args

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

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

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

>> > 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
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this a bug with @args RickB PowerShell 4 04-09-2008 07:58 AM
Help with $Args in script David Kriz PowerShell 2 02-12-2008 09:00 AM
$args.count IT Staff PowerShell 0 12-27-2007 08:53 PM
$args in runspace invoke William Stacey [C# MVP] PowerShell 0 05-09-2007 11:10 AM
-match within where-object against $args[0] Joe D PowerShell 2 12-04-2006 11:23 AM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51