ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Understanding $args in PowerShell

    Developer Discussion
    powershell
    7
    92
    70.5k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • scottalanmillerS
      scottalanmiller
      last edited by

      Fun. Lots of snow there in PA? I hope that the weather is good, I have to fly to San Francisco in the morning and I'm not far from PA.

      1 Reply Last reply Reply Quote 0
      • C
        chutestrate
        last edited by

        No snow. Just my part of the block. Have a safe trip

        1 Reply Last reply Reply Quote 0
        • scottalanmillerS
          scottalanmiller
          last edited by

          Thanks. Lifting off about noon and heading south first thing, so probably safe. Nothing is delayed so far.

          1 Reply Last reply Reply Quote 0
          • StrongBadS
            StrongBad
            last edited by

            This has been a great discussion. I'm learning a bit about PowerShell just from this.

            1 Reply Last reply Reply Quote 0
            • scottalanmillerS
              scottalanmiller
              last edited by

              Yes, very good discussion. Did I just see @Rob-Dunn pop in too?

              1 Reply Last reply Reply Quote 1
              • Rob DunnR
                Rob Dunn
                last edited by Rob Dunn

                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 DunnR 1 Reply Last reply Reply Quote 1
                • Rob DunnR
                  Rob Dunn @Rob Dunn
                  last edited by Rob Dunn

                  Youtube Video

                  1 Reply Last reply Reply Quote 2
                  • C
                    chutestrate
                    last edited by

                    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.

                    1 Reply Last reply Reply Quote 0
                    • C
                      chutestrate
                      last edited by

                      I need to correct my last post. I understand the named parameter demo, but not the $args piece of it.

                      1 Reply Last reply Reply Quote 0
                      • scottalanmillerS
                        scottalanmiller
                        last edited by

                        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.

                        1 Reply Last reply Reply Quote 1
                        • C
                          chutestrate
                          last edited by

                          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.

                          scottalanmillerS 4 Replies Last reply Reply Quote 0
                          • C
                            chutestrate
                            last edited by

                            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.

                            1 Reply Last reply Reply Quote 0
                            • scottalanmillerS
                              scottalanmiller @chutestrate
                              last edited by

                              @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.

                              1 Reply Last reply Reply Quote 0
                              • scottalanmillerS
                                scottalanmiller @chutestrate
                                last edited by

                                @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.

                                1 Reply Last reply Reply Quote 0
                                • scottalanmillerS
                                  scottalanmiller @chutestrate
                                  last edited by

                                  @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.

                                  1 Reply Last reply Reply Quote 0
                                  • scottalanmillerS
                                    scottalanmiller @chutestrate
                                    last edited by

                                    @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.

                                    1 Reply Last reply Reply Quote 0
                                    • C
                                      chutestrate
                                      last edited by

                                      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?

                                      scottalanmillerS 1 Reply Last reply Reply Quote 0
                                      • C
                                        chutestrate
                                        last edited by

                                        nm, i think i get it now

                                        1 Reply Last reply Reply Quote 0
                                        • scottalanmillerS
                                          scottalanmiller @chutestrate
                                          last edited by

                                          @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.

                                          1 Reply Last reply Reply Quote 0
                                          • C
                                            chutestrate
                                            last edited by

                                            so, arrays within arrays?

                                            coliverC JaredBuschJ scottalanmillerS 3 Replies Last reply Reply Quote 0
                                            • 1
                                            • 2
                                            • 3
                                            • 4
                                            • 5
                                            • 4 / 5
                                            • First post
                                              Last post