Understanding $args in PowerShell
-
Yes, very good discussion. Did I just see @Rob-Dunn pop in too?
-
I was on my phone; Martin sent me a PM over at SW
I had responded to this question over there in the Powershell forum, but chutestrate's account is in moderation for some reason.
Anyway - I'll try to take a stab at this.
As mentioned, when you run a script or a function, powershell has a built-in mechanism that will interpret anything passed after that script or function (that are not named parameters) and places them into an $arg *object.' It's kind of like a basket that catches the other stuff passed that hasn't been explicitly handled in the script/function. Since someone could feasibly pass more than one argument to the script, Powershell needs to have a way to handle more than one argument...just in case. With that being said, there's always an $arg object, whether you've passed arguments or not. It's just there to handle anything that fall outside named parameters during execution.
Remember, everything is an object in Powershell. It can represent one thing or many things. In this case, $arg is an object that represents many things; i.e an array. $arg is a representation of that array.
So, you might run a script like this:
test.ps1 -ComputerName <COMPUTER_NAME> -ProcessName <PROCESS_NAME> more arguments
or even like this:
test.ps1 -ComputerName <COMPUTER_NAME> -ProcessName more <PROCESS_NAME> arguments
Since we've specified -ComputerName and -ProcessName, those are named parameters (as denoted by the dash '-' followed by the name of the parameter you are passing.), so anything that is passed outside of those named parameters will be placed into the $arg object. In either of the above examples, $arg[0] would be 'more' and $arg[1] would be 'arguments.' Like Scott and Martin said, $arg is an array, and in the script/function, the [0] and [1] just helps identify which item in the array we want to return.
-
-
Rob thank you for doing that. I've watched it a few times, and I'm not catching what you are trying to demonstrate. I'm sorry.
-
I need to correct my last post. I understand the named parameter demo, but not the $args piece of it.
-
Are you able to describe in any way how you are finding $args confusing? I realize that when you don't understand something that there is really little way to describe how it is being misunderstood. If you knew what you didn't understand... you'd understand it. But I'm trying to come up with some way to figure out what is being missed. If you know what an array is, you know $args. So I'm wondering if arrays aren't understood or if you just aren't accepting that $args is just the name of an array or what.
-
Ok, I'm kinda with you at this point. Correct me if anything I'm thinking is incorrect. I'm using english, not the correct terms.
I understand that $args is automatic. I believe the mental block is the where it is an array, or is it creating and naming an array [$args[0], $args[1], etc.) Wait, you told me that the numbering of $args is how powershell is matching the automatic creation of $args to an array? So each auto creation of $args is an array?
I believe I'm ok with the concept of explicitly creating and naming an array.
What I'm having the heartburn over is how $args is an array, or creates an array, or was just put there to drive me a little crazy. Do you have to put it in the code you are developing? I've been given some code examples that included it. Were these just examples of what would look like or is it necessary to be used if not creating/naming an array. If it is automatic how do you use it later on?
Hopefully, these questions give some kind of hint to my confusion.
Thanks again for all the support.
-
sigh. I just don't think this is going to happen. I think it's been explained as many ways as possible, and I keep missing the message.
I think it's time to let this go.
The support has been terrific thank you.
-
@chutestrate said:
Wait, you told me that the numbering of $args is how powershell is matching the automatic creation of $args to an array? So each auto creation of $args is an array?There is an array. That array's name is $args. That is all.
Yes, each program gets one $args and it is always an array.
-
@chutestrate said:
What I'm having the heartburn over is how $args is an array.
This is where you are stuck. $args is an array. That is all that there is to it. It just is.
-
@chutestrate said:
Do you have to put it in the code you are developing?
No, not if you don't want to use it. You never have to use anything that you don't want to.
-
@chutestrate said:
If it is automatic how do you use it later on?
No idea what you mean. It's an array. Use it whenever you want. It isn't like arrays go bad after a period of time or something. It's just an array. As long as you know what an array is and that this is an array, that should explain everything. It's just an array. It's not required, it's not magic, it doesn't time out. It is absolutely exactly like every other array.
-
I think you are right. I'm stuck between $args and arrays. Fine, $args is an array to use or not use. Then $args[0] and $args[1] are different arrays?
-
nm, i think i get it now
-
@chutestrate said:
I think you are right. I'm stuck between $args and arrays. Fine, $args is an array to use or not use. Then $args[0] and $args[1] are different arrays?
No, in any array the array is named like this:
- $args
- $myarray
- $shoes
The use of [0], [1], etc. is to refer to specific positions within the array. So $args[0] is the first variable in the array. $args[1] is the second variable in the array and so forth.
-
so, arrays within arrays?
-
@chutestrate said:
so, arrays within arrays?
Not necessarily, just the value of the $args array at position 0.
-
not following.
-
@chutestrate said:
so, arrays within arrays?
NO.
$args[0] is the first item in the $args array.
$myarray[0] is the first item in the $myarray array.
$shoes[0] is the first item in the $shoes array.They are all just an array. $args is simply an array. nothing more nothing less.
-
An array is just an unordered list of objects. To call from an array in powershell you need to identify the array, in this case we use the identifier $args, and the location of the object you are trying to get, this can be anything like [0],[1].[2],[n] (n being a variable for any number).