Powershell function New-Array

Powershell can slice and dice arrays all day long, and we use them all the time. However, when we are testing stuff and creating dummy arrays the syntax can be a bit awkward. Its quite straightforward and understandable, but its just a pain to type

For example

   1: PS 39 >  $i = "one","two","three"
   2: PS 40 >  $i
   3: one
   4: two
   5: three

Now for me that is just way too many quotes and commas. This can easily be addressed with a new function called New-Array.  Are we going to have to parse and add quotes and do all kinds of crazy stuff to get this working ? Not really. Here’s the function and how to use it.

   1: PS 41 >  function new-array {$args}
   2: PS 42 >  $i = new-array one two three four five six
   3: PS 43 >  $i
   4: one
   5: two
   6: three
   7: four
   8: five
   9: six

Ah, now that is much better. All this does is return the $args array which is an array that you get in every function be default. $args[0] is the first argument. $args[1] is the second etc etc.

That being said we are not quite there.  Lets say you want to be really sure you create an Array. In the case where you originally have only 1 $arg it will return a scalar. Let me demonstrate.

   1: PS 58 >  function new-array {$args}
   2: PS 59 >  $i = new-array 1
   3: PS 60 >  $i += 2
   4: PS 61 >  $i
   5: 3

 

In this case, $i ends up being a scalar with a value of 1 and then when you add 2 to it, you get $i = 3.

What we want is to be able to use the += operator to add a value to the array. There are multiple ways to force a scalar to be an array when you declare it but i think the comma operator is the shortest.  So we make one quick addition to our new-array function.

   1: PS 62 >  function new-array {,$args}
   2: PS 63 >  $i = new-array 1
   3: PS 64 >  $i +=2
   4: PS 65 >  $i
   5: 1
   6: 2

Pretty cool!

Advertisements

27 Responses to “Powershell function New-Array”

  1. The 411 on Powershell Operators - Part 1 the -eq operator « Get-Powershell Says:

    […] Powershell function New-Array […]

  2. bedroom Says:

    hotels…

    […]Powershell function New-Array « Get-Powershell[…]…

  3. plumbing Tulsa Says:

    I’m still learning from you, but I’m improving myself. I definitely enjoy reading everything that is written on your website.Keep the aarticles coming. I loved it!

  4. http://tinyurl.com/whyaflora27906 Says:

    “Powershell function New-Array Get-Powershell”
    actually got myself hooked with ur page! Iwill wind up being back again
    far more normally. Thanks -Gabriele

  5. pretty green parka Says:

    What a information of un-ambiguity and preserveness of precious know-how on the topic of
    unpredicted emotions.

  6. hurtownia torebek Says:

    When some one searches for his essential thing, thus
    he/she desires to be available that in detail,
    therefore that thing is maintained over here.

  7. juicing for cancer patie Says:

    Howdy, I believe your web site may be having web browser compatibility problems.
    When I look at your website in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping issues.
    I simply wanted to give you a quick heads up! Aside from that, great site!

  8. http://www.olgogogo.com/upcoming.php Says:

    Hi my friend! I want to say that this article
    is awesome, great written and include almost all important
    infos. I would like to see extra posts like this .

  9. mortgage modification Says:

    This web site certainly has all of the information and facts
    I needed about this subject and didn’t know who to ask.

  10. Gerardo Says:

    I personally didn’t buy an organic bassinet mattress because my model came with the non-organic variety. This allows your babies to sleep close to each other without clobbering or rolling over each other. Test the mattress for firmness by pressing a finger to it.

  11. BLOOMFIELDLIVESTOCKMARKET.COM Says:

    It’s very simple to find out any topic on net as compared to textbooks, as I found this paragraph at this web site.

  12. Rapid Content Wizard reviews Says:

    Nice post. I was checking constantly this blog and I am impressed!
    Extremely useful information specially the last part 🙂 I care for such info much.
    I was seeking this particular information for a long time.
    Thank you and good luck.

  13. Moen tiskové cartridges 2 rukojeť Says:

    Sony LTX 100G The top Multimedia Cartridge The govt significant digital data are
    needed sensitive information and facts is significant possessions specially in today’s strict observance aura. This is exactly why market leaders with the state length in Sony Storage area Newspaper and tv with regard to respectable LTO storage containers facts. Sony’s LTO Ultrium cartridge delivers a continuous
    limit as well as rapidly safeguarded acceleration for
    purchase for vitally important as well as open files. Deciding
    upon Sony will make it sure using continuous and durable LTO in order to meet
    just about all organization storing necessitate. Moen Výměna cartridges záruka tonery from sally’s beauty supply tonery samsung ml-1740 micr tonery cartridge hp laserjet p3015 tonery cartridges for cash topné těleso regulátoru moen cartridge replacement instructions change cartridge hp deskjet 1000.

  14. tonery brno Žabovřesky Says:

    The reliable Sony LTO1 tape, works with just about all brand LTO Ultrium tape propels in addition to an automatic methods and accomplish high quality read and write ability.
    Resilience:A Sony LTX100G, is definitely perfectly planned to the office throughout quite high in depth problems along with designed wealthy in
    course product and also created with a bit of pretty remarkable established engineering.
    This firmly shut down casing also inhibits any
    outside difficulties and prevent to penetrate this dust straight into Sony LTO LTX100G, cartridge and even shield the interior piece almost all types of tough
    plus harsh issues. Sleva inkoustové cartridges HP lisa Verducci tonery cartridge valve
    definition cartridge world nancy 2 hp 6700 změna
    inkoustová cartridge Stahovák cartridges Lowes cartridge svět Puteaux federal cartridge careers.

  15. http://network.nature.com Says:

    I think people have a junk drawer where all the stuff you don’t want to throw in the trash ends up. They can withstand large falls and impacts that other watches would crumble under. The new model include much like the Tough Solar power source and digital compass.

  16. technonews2013.wordpress.com Says:

    Most computers utilized in business, and many utilized in the home, will no longer operate
    inside old self contained fashion. com provides complete
    solution of satellite services like satellite
    uplink, satellite downlink, broadcasting satellite, satellite communication
    with all the complete satisfaction. This is about 400 times the annual electrical output in the
    Hoover Dam.

  17. cheap nfl jersyes Says:

    F:\88\Is It Possible To Distinguish Real Trainer Bag From Scam One .txt

  18. replacement water filters Says:

    Unquestionably believe that which you said. Your favorite
    justification seemed to be on the web the simplest thing to be aware of.
    I say to you, I definitely get annoyed while people
    think about worries that they just don’t know about.
    You managed to hit the nail upon the top as well as
    defined out the whole thing without having side-effects , people
    could take a signal. Will probably be back to get more.
    Thanks

  19. cheap nfl jerseys Says:

    This few familiar offering artists include those lauded Brian Jungen. Famous to obtain molding common site objects into culturally significant icons, Jungen sets the icon high for his or cultural and creative counterparts. These well-known traditional Western side Coast mask is actually sculpted out pointing to nike air jordan shoes, a complete familiar object a great school-aged student has the potential to identify with. His work is specially much a comments on tradition convention the obstacles most typically associated with materialism and muscle mass production.

  20. florida criminal defense lawyer Says:

    Quality articles or reviews is the main to be a
    focus for the people to pay a visit the site, that’s
    what this web page is providing.

  21. cheap home audio amplifiers Says:

    But then it is possible to also just keep adding tracks and recording onto them while listening (in
    headphones naturally) on the earlier track(s) playing back.

    Watching HD movies and TV programs will give you the most effective experience in the event you have good quality speakers
    hooked up. How to produce money from home is actually a simple process.

  22. http://cosmeticdentist181.jigsy.com Says:

    Very good article. I’m experiencing some of these issues as
    well..

  23. gta 5 Says:

    Greate pieces. Keep posting such kind of info on your site.

    Im really impressed by it.
    Hey there, You have done a fantastic job. I will certainly digg it and in my view
    recommend to my friends. I’m confident they will be benefited from this web site.

  24. robertsfilter.info Says:

    I’m gone to inform my little brother, that he should also pay a
    quick visit this web site on regular basis to obtain updated from newest gossip.

  25. עמוד הבית Says:

    Hmm it appears like your website ate my first comment
    (it was super long) so I guess I’ll just sum it up what I submitted and say,
    I’m thoroughly enjoying your blog. I as well am an aspiring blog writer but I’m still new to the whole thing.
    Do you have any helpful hints for inexperienced blog
    writers? I’d really appreciate it.

  26. the psychopath test genre Says:

    I’m not sure why but this blog is loading extremely slow for me.
    Is anyone else having this issue or is it a issue on my end?
    I’ll check back later on and see if the problem still exists.

  27. sales@ibuyallvehicles.com Says:

    Yet another thing I would like to mention is that in lieu of trying to fit all your online degree courses on days that you finish work (since the majority people are fatigued when they return), try to find most of your sessions on the weekends and only a couple of courses for weekdays, even if it means taking some time away from your end of the week. This is beneficial because on the week-ends, you will be a lot more rested in addition to concentrated on school work. Thanks for the different tips I have realized from your blog.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: