How to Create and Manipulate Lists in PowerShell?

In this tutorial, I will explain how to create and manipulate lists in PowerShell. Lists in PowerShell are data structures that allow you to store and manage collections of items efficiently. You can use a PowerShell list while working with managing user data, configuring systems, or automating tasks, etc.

Why Use Lists in PowerShell?

Lists in PowerShell are dynamic and can hold items of different data types, including integers, strings, and objects. Unlike arrays, lists can grow and shrink dynamically, making them more flexible for various scripting tasks. This flexibility is particularly useful when dealing with unpredictable data sizes or when you need to add or remove items from your collection frequently.

Create a Basic List in PowerShell

Now, let me show you how to create a list in PowerShell.

To create a list in PowerShell, you can use the [System.Collections.Generic.List[<type>]] class. Here’s an example of creating a list of strings in PowerShell:

# Create a list of strings
$list = [System.Collections.Generic.List[string]]::new()

# Add items to the list
$list.Add("New York")
$list.Add("Los Angeles")
$list.Add("Chicago")

# Display the list
$list

In this example, we create a list of strings and add three city names: New York, Los Angeles, and Chicago. The ::new() method is used to instantiate the list.

Here is the output you can see in the screenshot:

Create a List in PowerShell

Read Create Custom Objects in PowerShell

Add Items to a List

You can use the Add() method to add an item to a PowerShell list. Here’s an example of adding more items to our existing list:

# Add more city names to the list
$list.Add("Houston")
$list.Add("Phoenix")
$list.Add("Philadelphia")

# Display the updated list
$list

This script adds three more cities to the list: Houston, Phoenix, and Philadelphia.

Remove Items from a List

You can remove items from a list using the Remove or RemoveAt methods. The Remove method removes the first occurrence of a specified item, while RemoveAt removes an item at a specified index. Here’s how to remove items:

# Remove a specific city
$list.Remove("Chicago")

# Remove a city at a specific index
$list.RemoveAt(1) # Removes "Los Angeles"

# Display the updated list
$list

After running this script, “Chicago” and “Los Angeles” are removed from the list.

Read Create and Use Dictionaries in PowerShell

Iterate Over a List

Iterating over a list allows you to perform actions on each item. You can use a foreach loop to iterate through the list in PowerShell:

# Iterate through the list and display each city
foreach ($city in $list) {
    Write-Output "City: $city"
}

This script outputs each city name in the list.

Sort a PowerShell List

You can sort a list using the Sort method. Here’s an example of sorting our list of cities:

# Sort the list alphabetically
$list.Sort()

# Display the sorted list
$list

This script sorts the city names in alphabetical order.

Read Create a Hashtable in PowerShell

Filter a PowerShell List

Filtering a list allows you to extract items that meet certain criteria. You can use the Where-Object cmdlet for this purpose:

# Filter cities that start with the letter 'P'
$filteredCities = $list | Where-Object { $_ -like "P*" }

# Display the filtered list
$filteredCities

This script filters and displays cities starting with the letter “P”.

Convert a List to an Array

Sometimes, you may need to convert a list to an array for compatibility with other PowerShell cmdlets. You can use the ToArray method:

# Convert the list to an array
$array = $list.ToArray()

# Display the array
$array

This script converts the list to an array and displays it.

Read Create Objects in PowerShell

PowerShell List Example: Manage User Data

Let’s consider a practical use case where you need to manage a list of user names in a system. Here’s how you can create, manipulate, and display user names using a list:

# Create a list to store user names
$userList = [System.Collections.Generic.List[string]]::new()

# Add user names to the list
$userList.Add("JohnDoe")
$userList.Add("JaneSmith")
$userList.Add("MichaelJohnson")

# Display the list of user names
Write-Output "User List:"
$userList

# Remove a user name
$userList.Remove("JaneSmith")

# Display the updated list
Write-Output "Updated User List:"
$userList

# Sort the user names
$userList.Sort()

# Display the sorted list
Write-Output "Sorted User List:"
$userList

# Filter user names that contain 'John'
$filteredUsers = $userList | Where-Object { $_ -like "*John*" }

# Display the filtered user names
Write-Output "Filtered User List:"
$filteredUsers

This script demonstrates how to manage a list of user names, including adding, removing, sorting, and filtering user names.

Conclusion

Lists in PowerShell are used for managing collections of data. In this tutorial, I explained how to create and manage lists in PowerShell with some examples.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.