Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
about_Arrays
In this articleShort descriptionDescribes arrays, which are data structures designed to store collections of items. Long descriptionAn array is a data structure that is designed to store a collection of items. The items can be the same type or different types. Beginning in Windows PowerShell 3.0, a collection of zero or one object has some properties of arrays. Creating and initializing an arrayTo create and initialize an array, assign
multiple values to a variable. The values stored in the array are delimited with a comma and separated from the variable name by the assignment operator ( For example, to create an array named
The comma can also be used to initialize a single item array by placing the comma before the single item. For example, to create a single item array named
You can also create and initialize an array by using the range operator (
As a result, When no data type is specified, PowerShell creates each array as an object array (System.Object[]). To determine the data type of an array, use the GetType() method. For example, to determine
the data type of the
To create a strongly typed array, that is, an array that can contain only values of a particular type, cast the variable as an array type, such as string[], long[], or int32[]. To cast an array, precede the variable name with an array type enclosed in brackets. For example, to create a 32-bit integer array named
As a result, the You can create arrays that are cast to any supported type in the .NET. For example, the objects that
The array sub-expression operatorThe array sub-expression operator creates an array from the statements inside it. Whatever the statement inside the operator produces, the operator will place it in an array. Even if there is zero or one object. The syntax of the array operator is as follows:
You can use the array operator to create an array of zero or one object. For example:
The array operator is useful in scripts when you are getting objects, but do not know how many objects you get. For example:
For more information about the array sub-expression operator, see about_Operators. Accessing and using array elementsReading an arrayYou can refer to an array by using its variable name. To display all the elements in the array, type the array
name. For example, assuming
You can refer to the elements in an array by using an index, beginning at position 0. Enclose the index number in brackets. For example, to display the first element in the
To display the third element in the
You can retrieve part of the array using a range operator for the index. For example, to retrieve the second to fifth elements of the array, you would type:
Negative numbers count from the end of the array. For example, "-1" refers to the last element of the array. To display the last three elements of the array, in index ascending order, type:
If you type negative indexes in descending order, your output changes.
However, be cautious when using this notation. The notation cycles from the end boundary to the beginning of the array.
Also, one common mistake is to assume You can use the plus operator (
Also, to list multiple ranges and individual elements you can use the plus operator. For example, to list elements zero to two, four to six, and the element at eighth positional type:
Iterations over array elementsYou can also use looping constructs, such as
The The
You can use a
Properties of arraysCount or Length or LongLengthTo determine how many items are in an array, use the Length property or its Count alias. Longlength is useful if the array contains more than 2,147,483,647 elements.
RankReturns the number of dimensions in the array. Most arrays in PowerShell have one dimension, only. Even when you think you are building a multidimensional array like the following example:
In this example, you are creating a single-dimensional array that contains other arrays. This is also known as a jagged array. The Rank property proved that this is single-dimensional. To access items in a jagged array, the indexes must be in separate brackets (
Multidimensional arrays are stored in row-major order. The following example shows how to create a truly multidimensional array.
To access items in a multidimensional array, separate the indexes using a comma ( Some operations on a multidimensional array, such as replication and concatenation, require that array to be flattened. Flattening turns the array into a 1-dimensional array of unconstrained type. The resulting array takes on all the elements in row-major order. Consider the following example:
The output shows that
Methods of arraysClearSets all element values to the default value of the array's element type. The In the following example
In this example,
ForEachAllows to iterate over all elements in the array and perform a given operation for each element of the array. The
ForEach(scriptblock expression)ForEach(scriptblock expression, object[] arguments)This method was added in PowerShell v4. Note The syntax requires the usage of a script block. Parentheses are optional if the scriptblock is the only parameter. Also, there must not be a space between the method and the opening parenthesis or brace. The following example shows how use the
Just like the For more information about the behavior of ArgumentList, see about_Splatting. ForEach(type convertToType)The
ForEach(string propertyName)ForEach(string propertyName, object[] newValue)The
ForEach(string methodName)ForEach(string methodName, object[] arguments)Lastly,
Just like the Note Starting in Windows PowerShell 3.0 retrieving properties and executing methods for each item in a collection can also be accomplished using "Methods of scalar objects and collections". You can read more about that here about_methods. WhereAllows to filter or select the elements of the array. The script must evaluate to anything different than: zero (0), empty string, There is one definition for the
Note The syntax requires the usage of a script block. Parentheses are optional if the scriptblock is the only parameter. Also, there must not be a space between the method and the opening parenthesis or brace. The
The acceptable values for
The following example shows how to select all odd numbers from the array.
This example show how to select the strings that are not empty.
DefaultThe If a
Note Both the Last
SkipUntilThe This means the returned collection contains both passing and non-passing items that have NOT been tested. The number of items returned can be limited by passing a value to the
UntilThe This means that you receive the first set of non-passing items from the The number of items returned can be limited by passing a value to the
Note Both
SplitThe If a The remaining objects, even those that PASS the expression filter, are returned in the second collection.
Note Both Get the members of an arrayTo get the properties and methods of an array, such as the Length property and the SetValue method, use the InputObject parameter of the When you
pipe an array to When you use the InputObject parameter, For example, the following command gets the members of the
You can also get the members of an array by typing a comma (,) before the value that is piped to the
Manipulating an arrayYou can change the elements in an array, add an element to an array, and combine the values from two arrays into a third array. To change the value of a particular element in an array, specify the array name and the index of the element that you want to change, and
then use the assignment operator (
You can also use the SetValue method of an array to change a value. The following example changes the second value (index position 1) of the
You can use the
Note When you use the It is not easy to delete elements from an array, but you can create a new array that contains only selected elements of an existing array. For example, to create the
To combine two arrays into a single array, use the plus operator (
As a result, the To delete an array, assign a value of
You can also use the Arrays of zero or oneBeginning in Windows PowerShell 3.0, a collection of zero or one object has the Count and Length properties. Also, you can index into an array of one object. This feature helps you to avoid scripting errors that occur when a command that expects a collection gets fewer than two items. The following examples demonstrate this feature. Zero objects
One object
Indexing support for System.Tuple objectsPowerShell 6.1 added the support for indexed access of Tuple objects, similar to arrays. For example:
Unlike arrays and other collection objects, Tuple objects are treated as a single object when passed through the pipeline or by parameters that support arrays of objects. For more information, see System.Tuple. Indexing .NET types that implement IDictionary<TKey, TValue>PowerShell doesn't call a type's true indexer for types that implement the generic By contrast, if you call the type's true indexer using The following example illustrates the difference.
Member-access enumerationStarting in PowerShell 3.0, when you use the member-access operator to access a member that does not exist on a list collection, PowerShell automatically enumerates the items in the collection and attempts to access the specified member on each item. For more information, see about_Member-Access_Enumeration. ExamplesThe following example creates two new files and stores the
resulting objects in the array variable
Member-access enumeration enables you to get values from items in a collection, but not to set values on items in a collection. For example:
To set the values you must use a method.
The
Caution Since the method is executed for each item in the collection, care should be taken when calling methods using member enumeration. See also
FeedbackSubmit and view feedback for Can an array hold multiple values of several different types of data simultaneously?An array can hold multiple values of several different data types simultaneously. Declaring an array reference variable does not create an array. To compare the contents of two arrays, you must compare the elements of the two arrays. Once an array is created, its size cannot be changed.
Can an array have multiple data types?No, we cannot store multiple datatype in an Array, we can store similar datatype only in an Array.
Can an array hold multiple values?Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
Can an array hold mixed data types?Yes, We can use mixed datatypes in a single array.
|