Arrays

An array contains a set of objects of the same type. Each entry in the array is called an element and each element has an index (position). Indexing in an array starts from 0.

Arrays are an important part of PowerShell. When the return from a command is assigned to a variable, an array will be the result if the command returns more than one object. For example, the following command will yield an array of objects:

$processes = Get-Process 
Array type:
In PowerShell, arrays are, by default, given the type System.Object[] (an array of objects where [] is used to signify that it is an array).
Why System.Object?
All object instances are derived from a .NET, type or class, and in .NET every object instance is derived from System.Object (including strings and integers). Therefore, an array of System.Object in PowerShell can hold just about anything.

Arrays in PowerShell (and .NET) are immutable, and the size is declared on creation and it cannot be changed. A new array must be created if an element is to be added or removed. The array operations described next are considered less efficient for large arrays because of the re-creation overhead involved in changing the array size.

We will explore creating arrays, assigning a type to the array, selecting elements, as well as adding and removing elements. We will also take a brief look at how arrays may be used to fill multiple variables, and finish off with a look at multi-dimensional arrays and jagged arrays.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.227.9