0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script for Linear Search with Example and Explanation

Use a loop to check each element in an array with foreach ($item in $array) { if ($item -eq $target) { return index }} to perform a linear search in PowerShell.
📋

Examples

Input[1, 2, 3, 4, 5], target=3
Output2
Input["apple", "banana", "cherry"], target="banana"
Output1
Input[10, 20, 30], target=40
Output-1
🧠

How to Think About It

To find a value in a list, check each item one by one from start to end. If the item matches the target, return its position. If none match, return -1 to show it is not found.
📐

Algorithm

1
Get the array and the target value.
2
Start from the first element and check if it equals the target.
3
If it matches, return the current index.
4
If not, move to the next element.
5
Repeat until the end of the array.
6
If no match is found, return -1.
💻

Code

powershell
function LinearSearch($array, $target) {
    for ($i = 0; $i -lt $array.Length; $i++) {
        if ($array[$i] -eq $target) {
            return $i
        }
    }
    return -1
}

# Example usage
$array = @(1, 2, 3, 4, 5)
$target = 3
$result = LinearSearch $array $target
Write-Output $result
Output
2
🔍

Dry Run

Let's trace searching for 3 in the array [1, 2, 3, 4, 5].

1

Check first element

Index 0: value 1, compare with 3 → no match

2

Check second element

Index 1: value 2, compare with 3 → no match

3

Check third element

Index 2: value 3, compare with 3 → match found, return 2

IndexValueCompare with TargetMatch?
013No
123No
233Yes
💡

Why This Works

Step 1: Loop through array

The for loop goes through each index from 0 to the last element.

Step 2: Compare each element

Inside the loop, -eq checks if the current element equals the target.

Step 3: Return index if found

If a match is found, the function returns the current index immediately.

Step 4: Return -1 if not found

If the loop finishes without finding the target, -1 is returned to indicate absence.

🔄

Alternative Approaches

Using foreach with manual index
powershell
function LinearSearchForeach($array, $target) {
    $index = 0
    foreach ($item in $array) {
        if ($item -eq $target) {
            return $index
        }
        $index++
    }
    return -1
}

$array = @("apple", "banana", "cherry")
$target = "banana"
$result = LinearSearchForeach $array $target
Write-Output $result
This method uses <code>foreach</code> and a manual counter; it is easy to read but slightly less direct than using a <code>for</code> loop.
Using .IndexOf() method
powershell
$array = @(10, 20, 30)
$target = 20
$result = [Array]::IndexOf($array, $target)
Write-Output $result
This uses built-in .NET method for arrays, which is simpler but only works on arrays, not all collections.

Complexity: O(n) time, O(1) space

Time Complexity

The script checks each element once, so time grows linearly with array size.

Space Complexity

No extra space is used besides a few variables, so space is constant.

Which Approach is Fastest?

Using the built-in .IndexOf() is fastest and simplest if working with arrays; manual loops offer more flexibility.

ApproachTimeSpaceBest For
For loop linear searchO(n)O(1)General arrays and lists
Foreach with manual indexO(n)O(1)Readability and simple collections
.IndexOf() methodO(n)O(1)Arrays with built-in support
💡
Use a for loop with index to easily return the position of the found item.
⚠️
Forgetting to return -1 when the target is not found causes the function to return nothing.