How to Find the Index of a String in an Array in PowerShell?

Recently, I wanted to locate the exact position of a string in a PowerShell array. In this tutorial, I will explain several proven methods to find the index of a string in an array using PowerShell.

Method 1: Using .IndexOf() with Arrays

The .IndexOf() method is a .NET array method that returns the index of the first occurrence of a specified value. This is my go-to for simple, case-sensitive searches in PowerShell arrays.

Example:

Let’s say I have an array of US states and want to find where “California” appears.

$states = @("New York", "California", "Texas", "Florida", "Illinois")
$index = [Array]::IndexOf($states, "California")
Write-Output $index

Here, [Array]::IndexOf() searches through the array and returns the index of “California”. If it’s not found, it returns -1. This method is fast and straightforward for most use cases, especially when working with exact matches.

Here is the exact output in the screenshot below:

Find the Index of a String in an Array in PowerShell

Check out Add an Element to the Beginning of an Array in PowerShell

Method 2: Using PowerShell’s For Loop

If you need more control, such as case-insensitive matching or partial string checks, a for loop is ideal. I often use this when working with user names or server lists where capitalization can vary.

Example:

Here is an example.

$users = @("john.smith", "Jane.Doe", "alex.johnson", "Emily.Jones")
$search = "jane.doe"
$foundIndex = -1

for ($i = 0; $i -lt $users.Length; $i++) {
    if ($users[$i].ToLower() -eq $search.ToLower()) {
        $foundIndex = $i
        break
    }
}
Write-Output $foundIndex

This loop checks each element, comparing in a case-insensitive way. If it finds a match, it records the index and exits. This method is flexible and great for scenarios where you need more than just a direct match.

You can see the exact output in the screenshot below:

How to Find the Index of a String in an Array in PowerShell

Read Add Value to an Array in a Function in PowerShell

Method 3: Using Where-Object with Indexing

If you prefer a more PowerShell-native approach, Where-Object can help filter arrays, and ForEach-Object can help grab indexes.

Example:

Let me show you an example.

$states = @("Ohio", "Georgia", "California", "Nevada", "Texas")
$search = "Texas"

$index = ($states | ForEach-Object {$_}) -as [System.Collections.ArrayList]
$foundIndex = $index.IndexOf($search)
Write-Output $foundIndex

By converting the array to an ArrayList, you can use the .IndexOf() method, which works well with PowerShell’s pipeline. This is handy when you’re processing data on the fly.

Check out Add Values to a Multidimensional Array in PowerShell

Method 4: Using Select-Object with Index Property

Sometimes, you want to find all matches and their indexes, especially in lists with potential duplicates. Here’s how I do it using Select-Object:

Example:

Here is an example.

$states = @("Texas", "California", "Texas", "Florida")
$search = "Texas"

$matches = $states | Select-Object -Index 0..($states.Length-1) | 
    Where-Object { $_ -eq $search }

$indexes = @()
for ($i = 0; $i -lt $states.Length; $i++) {
    if ($states[$i] -eq $search) {
        $indexes += $i
    }
}
Write-Output $indexes

This finds all indexes where “Texas” appears. It’s perfect for lists with duplicates, like server logs or repeated user entries.

Check out Add Only Unique Values to an Array in PowerShell

Examples

Now, let me show you some real examples.

Find Substrings in Array Elements

Sometimes, you need to find an index where an element contains a substring, not an exact match. Here’s how I handle it:

$states = @("North Carolina", "South Carolina", "California", "Arizona")
$search = "Carolina"

$foundIndexes = @()
for ($i = 0; $i -lt $states.Length; $i++) {
    if ($states[$i] -like "*$search*") {
        $foundIndexes += $i
    }
}
Write-Output $foundIndexes

This approach is useful for partial matches, like searching for all states containing “Carolina”.

Case-Insensitive Index Search (Reusable Function)

Let’s wrap everything into a reusable function for your scripts:

function Get-StringIndex {
    param (
        [string[]]$Array,
        [string]$Search
    )
    for ($i = 0; $i -lt $Array.Length; $i++) {
        if ($Array[$i].ToLower() -eq $Search.ToLower()) {
            return $i
        }
    }
    return -1
}

# Usage
$index = Get-StringIndex -Array $states -Search "california"
Write-Output $index

Reusable functions save time and help standardize your scripts across projects.

Conclusion

In this tutorial, I explained how to find the index of a string in a PowerShell array using various methods, such as .IndexOf() method with examples.

Try all the above examples and do let me know in the comments below if it helps.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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