This blog will demonstrate how to create a PowerShell list. Moreover, we will also explain the procedure of how to add and remove items from it. So, let’s start!
How to create a PowerShell List?
A PowerShell list is a type of data structure that stores different types of items. The list’s size is not fixed and can be changed anytime.
To create a PowerShell list, use the syntax listed below.
Syntax
You can follow either of the given syntax for creating a list in PowerShell:
OR
Here, $ListName represents the name of the PowerShell list that will be created.
Example 1
In the given example, we will create a list named $PowerShelllist using the first syntax format:
To get the type of created list, we will use the Get-Type cmdlet:
The given output indicates that the type of the created list is System.Collection.ArrayList:
Example 2
In this example, we will create a list named $List1 by utilizing the second syntax format:
After creating the PowerShell list, you may need to perform some operations on it, such as adding, displaying, or removing elements. For this purpose, check out the following section.
PowerShell list methods
The below-given table enlisted some of the PowerShell list methods along with their description:
Methods | Description |
---|---|
Add() | To add items to the list, use the Add() method. |
Remove() | For removing items from the list, we use the Remove() method. |
Clear() | The Clear() method is used to clear items from the list. |
Clone() | The replica of a list is created by using the Clone() method. |
CopyTo() | To copy content from one list to another, use the CopyTo() method. |
Foreach() | The Foreach() method is used for iterating over the created list. |
GetType() | To find the type of list, use the method GetType(). |
Let’s look at an example where we will apply some of the given methods on a PowerShell list.
Let’s move ahead and add some items to the created PowerShelllist.
How to add an item in PowerShell?
In this example, we will add some items to the PowerShell list using the Add() method.
Syntax
In the above-given syntax, specify the value you need to add to the $ListName with the help of the Add() method.
Example
We will now utilize the Add() method for adding the following five items to our already created $PowerShelllist:
> $PowerShelllist.Add("John")
> $PowerShelllist.Add("2")
> $PowerShelllist.Add("38")
> $PowerShelllist.Add("Engineer")
Output
How to view items in a PowerShell list?
To view the list of the added items on the PowerShell list, execute the given Write-Host cmdlet:
The given output verifies that we have successfully added five items: 1, John, 2, 38, and Engineer:
Want to remove an existing item from the PowerShell list? Follow the provided section.
How to remove an element in PowerShell?
As mentioned earlier, the Remove() method can be utilized for removing existing items from a PowerShell list.
Syntax
For deleting or removing an element from the list using the Remove() method, use the following syntax:
Example
In this example, we will delete the Engineer item from our $PowerShelllist, using the Remove() method:
Again, run the Write-Host command to enlist the PowerShelllist items:
As you can see, the Engineer item does not exist in our list:
We have compiled important details about the PowerShell list. Hope that it will be helpful for you!
Conclusion
A PowerShell list is a data structure used for storing multiple items of different data types. To create a list in PowerShell, you must create an object of the System.Collections.ArrayList class. It also offers different methods to perform various operations, such as adding and removing items from the list. In this post, we discussed PowerShell List, its related methods, and their description.