PowerShell: How to create an empty array!

 

Hello everyone.

I often do training or work in PowerShell, and one thing I always forget is how to create an empty array.

I usually spend some time searching the net, without luck, and always end up opening an old script, and get the code from there!

On place to start with array info is this one:

http://technet.microsoft.com/en-us/library/ee692797.aspx

It has all the info you need also how to create an empty array.

That’s it!

and now you can add more to it by using +=

another great tip is to always use this method when adding to an array

Notice the , in front of $user, this makes sure that even though $array might not be an array at this time (might be null if it was not initialized), we make sure that this $user string is added to a array collection, and not inserted as the value of $array.

By | 2011-09-26T13:24:56+00:00 September 26th, 2011|Powershell, Scripting & Development|7 Comments

About the Author:

Jakob Gottlieb Svendsen

Twitter: @JakobGSvendsen

Jakob Gottlieb Svendsen is a Microsoft Cloud and Data Center Management MVP (http://mvp.microsoft.com/en-us/default.aspx), Working as Global Lead Developer, Senior Consultant and Trainer at CTGlobal, where he is one of the driving forces in keeping CTGlobal a System Center Gold Partner and member of the System Center Alliance.

Since he started at Coretech in 2007, he has focused on Scripting and Development, primarily developing tools, extensions and scripts for the System Center Suite. His main area is Automation (including OMS/Azure Automation, Service Management Automation, PowerShell and Orchestrator). Another area is Windows Azure Pack / Azure Stack, where he does implementation, development, workshops and presentations. He is a world-wide renowned voice in the Automation field.

He is passionately devoted to the community, to which he contributes by being a moderator at TechNet and sharing his knowledge at http://blog.ctglobalservices.com/jgs

  • Co-founder: PowerShell User Group Denmark
  • Speaker at MMS 2016, Minneapolis (www.mmsmoa.com)
  • SCU Europe 2014, 2015, 2016 (www.systemcenteruniverse.ch)
  • Microsoft TechEd North America 2014, Houston
  • NIC 2012,2013,2014,2015, Oslo (www.nic.com)
  • Microsoft CampusDays 2011, 2013, Copenhagen
  • Microsoft TechDays 2015, Sweden (www.techdays.se)
  • Microsoft Partner Event: New in SC2012 SP1
  • User group meetings (PSUG.DK , SCUG.DK/BE/NO, AZMUG + more)
  • Microsoft Certified Trainer.
  • Microsoft Scripting Guys Forum Moderator

Main working areas:

  • Automation (Azure Automation, SMA, SCO)
  • Windows Azure Pack / Azure Stack
  • System CenterVisual Studio Team Services / Team Foundation Server
  • Development:C#.Net, VB.NET, VBScript, PowerShell, Service Manager, OpsMgr, ConfigMgr
  • Orchestrator
  • Windows Azure Pack / Azure Stack

Training:

  • Azure Automation
  • Service Management Automation
  • System Center Orchestrator
  • PowerShell, VBScript, C#.Net, VB.Net
  • Windows Azure Pack / Azure Stack Development Workshops

7 Comments

  1. Chris April 25, 2014 at 17:06 - Reply

    Outstanding, Jakob! TechNet and Et Al. (many others) have missed this glaring tip. Thanks

  2. joe.light June 17, 2014 at 14:11 - Reply

    thy 4 the short-cut 😉

  3. Matthew June 9, 2015 at 19:43 - Reply

    Exactly what I needed! Thanks!

  4. […] Gottlieb Svendsen: creating an empty array and Microsoft TechNet: even more things you can do with […]

  5. Manikandan R March 1, 2016 at 13:51 - Reply

    Thanks man.. 🙂 This is what i was looking..

  6. Sravani March 11, 2016 at 14:37 - Reply

    You literally saved me!!

Leave A Comment