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

    Understanding $args in PowerShell

    Developer Discussion
    powershell
    7
    92
    71.0k
    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.
    • C
      chutestrate
      last edited by

      You guys are right about needing to understand arrays better i guess. I just started this last week, and thought i had at least a slight understanding of it. I can follow the scripting commands in the first line, but what is the second line doing?

      At least i can make ps1 files, and get it into powershell, lol

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

        @chutestrate said:

        I can follow the scripting commands in the first line, but what is the second line doing?

        The second line is just printing out the name of the argument that we are on and what the value of that argument is for you to see. When you look at the output you can see it patch up. You get to see the name of the argument like $args[0] and the value held by that argument.

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

          An array, as we discussed via PM, is an "array of things", like a list. As we are programming here, that is really a list of variables. So in the same way that a normal variable can have something in it such as a string, so can an individual array element. Here is an example:

          $myname = "John"
          

          or as a single element array:

          $myarray[0] = "John"
          

          These two things accomplish basically the same thing. You have stored your name in a variable. Now what if we want to store a second name?

          $yourname = "Peter"
          

          or as part of the array that you already made:

          $myarray[1] = "Peter"
          

          Again, we did the same thing. But to work with the plain variables we need to know two variable names. To do the same thing with the array we need only know one array name. Doesn't make much difference when we have only two. But what if we had ten, or a hundred, or an indefinite number of names to store? Then an array because way, way easier to manage because it can be of arbitrary size holding just one name or thousands.

          Each has its place, but arrays are a very important way to hold data in a program.

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

            So think of an array as a list of variables that don't have their own names. They are just "first array element", "second array element", "third array element" and so forth until the list is complete. If you don't know how many array elements you have it is easy to do a for loop through them...

            foreach thing in my array do something
            

            That's great because you need only know the array name. The foreach loop figures out how many things are in the array and does something with each one. If you had hundreds or thousands of individual variables that you wanted to do the same thing with, that would be a mess.

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

              That makes sense.

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

                For my current limited understanding arrays are easier to understand than than the $args

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

                  @chutestrate said:

                  For my current limited understanding arrays are easier to understand than than the $args

                  $args is just an array, nothing more.

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

                    Here is a little array program to do basically the same thing as above but using an array defined inside of the script rather than passing in arguments from the command line...

                    $myarray = "John", "Peter", "Linda", "Scott", "Norman", "Jenny"
                    
                    for ($i=0; $i -lt $myarray.length; $i++) {
                       'This is $myarray[' + $i + "], which is: " + $myarray[$i]
                    }
                    
                    1 Reply Last reply Reply Quote 0
                    • scottalanmillerS
                      scottalanmiller
                      last edited by

                      When you run it you get this...

                      > .\arraydemo.ps1
                      This is $myarray[0], which is: John
                      This is $myarray[1], which is: Peter
                      This is $myarray[2], which is: Linda
                      This is $myarray[3], which is: Scott
                      This is $myarray[4], which is: Norman
                      This is $myarray[5], which is: Jenny
                      
                      1 Reply Last reply Reply Quote 0
                      • C
                        chutestrate
                        last edited by

                        @scottalanmiller said:

                        @chutestrate said:

                        For my current limited understanding arrays are easier to understand than than the $args

                        $args is just an array, nothing more.

                        don't follow that one.

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

                          So as you can see, I set the array to have those names in it with this line...

                          $myarray = "John", "Peter", "Linda", "Scott", "Norman", "Jenny"
                          

                          When you use $args, the exact same thing is happening except that PowerShell is doing it automatically and hidden from you because it is happening from the command line and you don't write it out explicitly. But what is happening under the hood in a case where you do this...

                          .\myexample John Peter Linda Scott Norman Jenny
                          

                          What PowerShell is doing without you having to write it is basically this...

                          $args = "John", "Peter", "Linda", "Scott", "Norman", "Jenny"
                          
                          1 Reply Last reply Reply Quote 0
                          • scottalanmillerS
                            scottalanmiller @chutestrate
                            last edited by scottalanmiller

                            @chutestrate said:

                            $args is just an array, nothing more.

                            don't follow that one.

                            Not sure what else to say. It's that simple. $args is an array. $myarray is an array. There's nothing more to it. The only thing that makes $args special is that it is the one array that PowerShell makes for you when you run your script.

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

                              Not a problem. This has been phenomenal. I've learned a lot. I'll try to use this to understand the args. Unfortunately, I'm looping back to some of my original confusion. I know you addressed it, but it's not making completed sense.

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

                                Definitely leave $args for now and focus on arrays. Once you are solid on arrays, $args is pretty much self explanatory. Try doing some sample scripting with arrays for things. That will help a lot. In all seriousness, doing a MadLibs script is a good way to learn.

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

                                  I'll do that.

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

                                    I doubt that Martin will agree but I don't find PowerShell to be particularly well suited to learning programming concepts. It's a great language for what it is, but I'm very glad that I learned programming on other languages and then learning PowerShell.

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

                                      I'm not fooling myself. I don't do well with trying to be a programmer. This is part of the testing goals. I just got interested in powershell, and am trying it out. Thank you for all the time. I'm rereading the posts, and I'm going to have a hard time matching $args to arrays. Maybe tomorrow it will click.

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

                                        @chutestrate said:

                                        I'm rereading the posts, and I'm going to have a hard time matching $args to arrays. Maybe tomorrow it will click.

                                        Just remember that there is nothing to match. $args is just another array like any other. It's not a different thing. It's not "like" an array, or similar to or anything. It is an array. $args is the name of an array. I have a feeling that you are thinking of $args as a thing rather than the name of an array.

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

                                          I definitely am. Unfortunately, i don't see it the way you are telling me.

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

                                            @scottalanmiller said:

                                            I doubt that Martin will agree but I don't find PowerShell to be particularly well suited to learning programming concepts. It's a great language for what it is, but I'm very glad that I learned programming on other languages and then learning PowerShell.

                                            The languages I've mostly worked with are batch/cmd, Visual Basic, vbScript and PowerShell. So PowerShell is a vast improvement over those 🙂

                                            It took me awhile to really grasp the idea of objects and using them for everything in PowerShell, but once I did the whole language opened up to me and I began to realize all the things I could do.

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