Often you may want to use PowerShell to find the index of a specific string in an array.
If you know that the string only occurs once, then you can use the following syntax to find the index position of the string:
[array]::IndexOf($my_array, "my_string")
If the string may occur multiple times in the array, then you can use the following syntax to find each index position of the string:
(0..($my_array.Count-1)) | where {$my_array[$_] -eq "my_string"}
The following examples show how to use each method in practice.
Example 1: Find Index of String in Array When String Only Occurs Once
Suppose that we create the following array in PowerShell named $my_array that contains the names of various basketball teams:
$my_array = @("Mavs", "Nets", "Pacers", "Hawks", "Kings")
We can use the following syntax to find the index position of the string “Hawks” in this array:
[array]::IndexOf($my_array, "Hawks")
The following screenshot shows how to use this syntax in practice:

The output tells us that the string “Hawks” occurs in index position 3 of the array.
Note: Keep in mind that the first string in the array has an index position of 0.
Example 2: Find Index of String in Array When String Occurs Multiple Times
Suppose that we create the following array in PowerShell named $my_array that contains the names of various basketball teams:
$my_array = @("Hawks", "Nets", "Pacers", "Hawks", "Hawks")
We can use the following syntax to find the index position of the string “Hawks” in this array:
(0..($my_array.Count-1)) | where {$my_array[$_] -eq "Hawks"}
The following screenshot shows how to use this syntax in practice:

The output tells us that the string “Hawks” occurs in index positions 0, 3 and 4 of the array.
Related Tutorials
The following tutorials explain how to perform other common tasks in PowerShell:
PowerShell: How to Find Position of Character in String
PowerShell: How to Replace Text in String
PowerShell: How to Concatenate Strings
