How to Replace a String in an Array Using PowerShell?

Do you want to replace a string in a PowerShell array? In this PowerShell tutorial, I will explain how to replace a string in an array using PowerShell.

To replace a string in a PowerShell array, use a loop to iterate through the elements and conditionally assign a new value when a match is found. For example, with $myArray -eq 'oldString', use $myArray[$i] = 'newString' within a for loop to replace ‘oldString’ with ‘newString’. This method ensures that each instance of the string is replaced throughout the array.

Replace a String in an Array Using PowerShell

First, let’s create an array in PowerShell. An array is a data structure that stores a collection of items. You can create an array by assigning multiple values to a variable:

$myArray = 'apple', 'banana', 'cherry', 'date'

Decide which string you want to replace. For example, let’s say we want to replace 'banana' with 'blueberry'.

To replace a string in an array, you can use a foreach loop to iterate through each item or use the for loop if you need to know the index. Here’s how you can do it with a foreach loop:

for ($i = 0; $i -lt $myArray.Length; $i++) {
    if ($myArray[$i] -eq 'banana') {
        $myArray[$i] = 'blueberry'
    }
}

And here’s with a foreach loop:

foreach ($item in $myArray) {
    if ($item -eq 'banana') {
        $index = [array]::IndexOf($myArray, $item)
        $myArray[$index] = 'blueberry'
    }
}

PowerShell also provides a -replace operator that you can use to replace text within strings. This can be combined with a foreach loop to replace strings within an array:

$myArray = $myArray | ForEach-Object { $_ -replace 'banana', 'blueberry' }

This command passes each element of $myArray through a pipeline, and for each object ($_), it replaces 'banana' with 'blueberry'.

After running the replacement, you can check the contents of the array to ensure the string has been replaced:

$myArray

This will output the contents of the array, and you should see 'blueberry' in place of 'banana'.

Below is a complete PowerShell script demonstrating how to replace a specific string within an array.

# PowerShell Script to Replace a String in an Array

# Define the array with initial values
$myArray = 'apple', 'banana', 'cherry', 'date'

# Display the original array
Write-Host "Original Array: " -ForegroundColor Green
$myArray

# The string to search for
$searchString = 'banana'

# The string to replace with
$replacementString = 'blueberry'

# Loop through the array and replace the string
for ($i = 0; $i -lt $myArray.Length; $i++) {
    if ($myArray[$i] -eq $searchString) {
        $myArray[$i] = $replacementString
    }
}

# Alternatively, using the -replace operator with ForEach-Object
# $myArray = $myArray | ForEach-Object { $_ -replace $searchString, $replacementString }

# Display the modified array
Write-Host "Modified Array: " -ForegroundColor Cyan
$myArray

Once you run the code, you can see the output in the screenshot below:

powershell replace string in array

Conclusion

In this PowerShell tutorial, you’ve learned how to replace a string in an array in PowerShell.

You may also like:

>