Back to Basics: Arrays

In PowerShell, arrays are a wonderfull and very powerfull tools which will help you in your daily labor. Today, i wanna speak about that, returning to the basics and help you mastering it.

Mosts of your scripts deal with multiple servers, files, and more. PowerShell arrays can’t be used like any other data types, you can create, add data, remote it, sort it.

Create an Array

To create a basic array, just declare a variable wih items set separated by commas.

Kinda easy isn’t it !

Another way to create an array…

Playing with arrays 

You see, that you can store multi data type in the same array. let’s see methodes available with this array…

Ohoh, you see it ? If you look for the properties, or methods of an array containing Integers or Strings, you’ll list all theses members properties, but nothing about Arrays…  Array is not an Array !

 

If you want to get the members of an array, use the comma before the variable

Great this is what we were looking for.

Note that you can also do the following !

If you want to know the hierarchie of an array (works also for any kind of variables)

Now let’s have a look about the hierarchie

 

Sort an array

Let’s play with the Sort method to start

You get an exception. It’s normal, you can’t use the Sort method in an array, where multiple datas types exist. By the way, if you still want to sort a multi typed array, PowerShell has (always) a solution. To sort an array of multi types, use the Sort-Object cmdlet !

Access elements of an array

If  you want access specifics elements in an array.

You can also specify range

And you can of course use a range and an unique index !

Now you want to view access each elements

If you want to confirm an array contains an item, you can also use the comparison operators:

  • -Contains and -in whould be used for searching a specific element
  • -match or -like to use a pattern to search

Add elements in an array 

Adding elements to an array isn’t really difficult, the classical way is to use the Add method.

Another way is to add full range of values

You can also add an array to another array.

 

Remove elements from an array

You can remove a set of elements using comparison operators and store the result in an array :p

pretty usefull isn’t it ?

You can of course remove a specific row from an array

You can also remove a specific value from the array

 Transform a string in an array

It’s very easy (it’s PowerShell) to transform a String in a complete array. To do this task we’ll use the Split() method available on string based objects…

 

 

Jagged arrays VS Multidimensional arrays

Both are usefull for holding lists of lists or arrays of arrays. Jagged is faster and use less memory than multidimensional, because it contains only the number of elements it needs. A non jagged array, is more like a matrix where every array must be the same size.

Here is how craete a jagged array.

And now, let’s have a look on multidimensional array. Let’s create a two dimension array.

If you wanted to create a three dimension array…

Ok, i hope this article talks to you and you’ll use more and more arrays in your scripts 🙂

If you have any question about arrays, do not hesitate to ask in the comment part, also, if you find errors in this article say it !

 

Have a nice week end !

Regards