Arrays in PowerShell are a fundamental data structure that stores a collection of items, which can be of the same type or different types. This tutorial will guide you through the basics of creating and using arrays in PowerShell. Also, you will get tons of tutorials related to PowerShell Array in this post.
PowerShell Array
An array in PowerShell is a data structure that stores a collection of items. These items can be of the same type or different types. In PowerShell, arrays are incredibly flexible and can hold anything from strings and integers to objects and even other arrays. This makes them particularly useful for storing and manipulating sets of data.
Create an Array in PowerShell
To create an array in PowerShell, you simply assign multiple values to a variable, separated by commas:
$myArray = 1, 2, 'three', 4, 'five'This creates an array with five elements, including both integers and strings.
Using the @() Array Subexpression Operator
Another way to create an array is by using the array subexpression operator @():
$myArray = @(1, 2, 3, 4, 5)This explicitly creates an array of five integers in PowerShell.
Access Array Elements
Each item in a PowerShell array is assigned an index number, starting at 0 for the first element. You can access an array element by specifying its index inside square brackets:
$thirdItem = $myArray[2]This retrieves the third item in the array, which in our example would be the integer 3.
Modifying Arrays
You can modify an existing PowerShell array by assigning a new value to a specific index:
$myArray[2] = 'three'Now, the third element in the array is the string 'three' instead of the integer 3.
Adding and Removing Items
To add an item to an array, you can use the += operator:
$myArray += 6This appends the integer 6 to the end of the array.
To remove an item, you can use the Remove-Item cmdlet or filter the array:
$myArray = $myArray | Where-Object { $_ -ne 'three' }This command filters out the string 'three', effectively removing it from the array.
- How to Add Values to an Array in PowerShell?
- How to Add Only Unique Values to an Array in PowerShell?
- How to Add Values to a Multidimensional Array in PowerShell?
- How to Add Value to an Array in a Function in PowerShell?
- How to Add an Element to the Beginning of an Array in PowerShell?
Iterating Through Arrays
You can iterate through each item in an array using a foreach loop:
foreach ($item in $myArray) {
Write-Host "Item: $item"
}This will print each item in the array to the console.
Sorting Arrays
PowerShell makes it easy to sort arrays with the Sort-Object cmdlet:
$sortedArray = $myArray | Sort-ObjectThis sorts the array in ascending order.
Multidimensional Arrays
PowerShell also supports multidimensional arrays, which are arrays of arrays:
$multiArray = @( @(1,2,3), @('a','b','c'), @(True,False) )Here, $multiArray is an array with three elements, each of which is an array.
PowerShell Array Tutorials
Here is the list of PowerShell array tutorials; you can read to learn everything about PowerShell array and various operations.
- PowerShell Array vs ArrayList
- Create an Empty Array in PowerShell
- Create Empty Array Of Objects In PowerShell
- Create an Array Of Objects In PowerShell
- How to Create Byte Arrays in PowerShell?
- Dynamic Arrays in PowerShell
- Associative Arrays in PowerShell
- PowerShell Hashtable vs Array
- How to Sort an Array in PowerShell?
- PowerShell Append to Array
- Array Comparisons in PowerShell
- Compare Two Arrays for Missing Elements in PowerShell
- Compare Two Arrays for Matches in PowerShell
- Export an Array to CSV in PowerShell
- How to Print Arrays in PowerShell?
- How to Join Arrays in PowerShell?
- How to Get the First Element of an Array in PowerShell?
- How to Select the Last Item in an Array in PowerShell?
- How to Remove the Last Element from an Array in PowerShell?
- Remove Items from an Array in PowerShell
- How to Remove Duplicates from an Array in PowerShell?
- How to Get Array Length in PowerShell?
- PowerShell If Array Length Greater Than Example
- How to Split Strings into Arrays in PowerShell?
- How to Loop Through an Array in PowerShell?
- How to Convert an Array to a String in PowerShell?
- PowerShell Multidimensional Arrays
- PowerShell Array Parameters
- How to Split an Array into Smaller Arrays in PowerShell?
- Sort Array Alphabetically In PowerShell
- Check if a String Exists in an Array in PowerShell
- Create Character Array In PowerShell
- Remove Blank Lines from an Array in PowerShell
- How to Find Duplicates in an Array in PowerShell?
- How to Pick Random Items from an Array in PowerShell?
- How to Clear an Array in PowerShell?
- How To Create Empty Array And Add Items In PowerShell?
- How to Replace Values in an Array in PowerShell?
- How to Get Unique Values from an Array in PowerShell?
- How to Find a String in an Array in PowerShell?
- How to Check if an Array Contains More than One Value in PowerShell?
- How to Reverse An Array in PowerShell?
- How to Read Excel Files into an Array in PowerShell?
- How to Join an Array into a String in PowerShell?
- How to Sum All Numbers in an Array in PowerShell?
- PowerShell format array as Table
- Expand Array in PowerShell
- Array Contains in PowerShell
- How to Import CSV to Array in PowerShell?
- How to Check if an Array Contains Another Array in PowerShell?
- How to Check if an Array is Empty in PowerShell?
- How to Create an Object Array with Properties in PowerShell?
- How to Convert String to Byte Array in PowerShell?
- How to Check If an Item Does Not Exist in an Array in PowerShell?
- How to Check if an Array Contains a String Case-Insensitive in PowerShell?
- How To Check If Array Contains Part Of String In PowerShell?
- How To Convert Byte Array To String In PowerShell?
- How to Check if an Array Does Not Contain a String in PowerShell?
- PowerShell Ordered Arrays
- PowerShell If Array Contains
- Read File Line By Line Into Array In PowerShell
- Read JSON File into Array in PowerShell
- How to Convert an ArrayList to an Array in PowerShell?
- How to Check an Array for Two Conditions in PowerShell?
- How To Add Multiple Values To Array In PowerShell?
- How to Sort an Array of Objects in PowerShell?
- How to Convert Object to Array in PowerShell?
- How to Convert Multiline String to Array in PowerShell?
- How to Remove Array Element by Index in PowerShell?
- How to Replace Multiple Strings in an Array Using PowerShell?
- How to Format an Array as an HTML Table in PowerShell?
- Find the Index of a String in an Array in PowerShell
- PowerShell Convert Byte Array to Hex String
- Find First Match in Array and Return Value in PowerShell
- PowerShell Cannot Index into a Null Array
- How to Convert Arrays to Hashtables in PowerShell?
- How to Join an Array of Strings in PowerShell