How to Replace Values in an Array in PowerShell?

Do you need to replace or update values in a PowerShell array? In this PowerShell tutorial, we’ll explore how to replace values in an array in PowerShell using different methods with examples.

To replace values in an array in PowerShell, you can use a ForEach-Object loop or the -replace operator. For example, $array = $array | ForEach-Object { $_ -replace ‘oldValue’, ‘newValue’ } will replace all occurrences of ‘oldValue’ with ‘newValue’ in the array. If the array contains simple strings and you want to replace a specific value, you can directly set the value using an index, like $array[0] = ‘newValue’ if you want to replace the first element.

Replace Values in an Array in PowerShell

Here is an example of an array in PowerShell.

$myArray = 1, 2, 3, 4, 5

Now, let’s say you want to replace a value in the above array in PowerShell. There are several methods to do this, and we’ll cover each one with examples.

1. Using Index Assignment

The simplest way to replace an item in an array in PowerShell is by using its index. Arrays are zero-indexed, which means the first item has an index of 0, the second item an index of 1, and so on. Here’s how you can replace the second item (value 2) with a new value (10):

$myArray = 1, 2, 3, 4, 5
$myArray[1] = 10

You can see here below that after I executed the PowerShell script using VS code, the 2nd item (index – 1) was replaced with the value 10.

Replace Values in an Array in PowerShell

2. Using the -replace Operator

PowerShell also provides the -replace operator, which can be used to replace specific text or characters in an array. This is useful when you want to replace parts of string values. For example:

$myArray = "cat", "bat", "rat"
$myArray = $myArray -replace 'at','oy'
$myArray

After this operation, $myArray will contain “coy”, “boy”, “roy”.

3. Using a Loop to Replace Values

Sometimes, you might need to replace values based on a condition. In such cases, a loop can be handy. For example, replacing all instances of the number 2 with the number 20. Here is the complete PowerShell script to replace a value in an array using a for loop.

$myArray = 1, 2, 3, 4, 5, 2
for ($i = 0; $i -lt $myArray.Length; $i++) {
  if ($myArray[$i] -eq 2) {
    $myArray[$i] = 20
  }
}
$myArray

Once you execute the PowerShell script, it will display the output like in the screenshot below:

How to Replace Values in an Array in PowerShell

4. Using the ForEach-Object Cmdlet

The ForEach-Object cmdlet is a more PowerShell-idiomatic way of performing the same action as a loop. Here’s how you can use it to replace values in an array in PowerShell.

$myArray = 1, 2, 3, 4, 5, 2
$myArray = $myArray | ForEach-Object {
  if ($_ -eq 2) {
    20
  } else {
    $_
  }
}
$myArray

5. Using a Custom Function

For more complex scenarios or frequent array value replacements, you might want to define a custom function; here, you can see that I wrote a custom function that will take two parameters (old value, new value) and finally it will replace the new value with the old value in the PowerShell array.

function Replace-ArrayValue {
  param(
    [array]$Array,
    [object]$OldValue,
    [object]$NewValue
  )

  for ($i = 0; $i -lt $Array.Length; $i++) {
    if ($Array[$i] -eq $OldValue) {
      $Array[$i] = $NewValue
    }
  }

  return $Array
}

# Usage
$myArray = Replace-ArrayValue -Array $myArray -OldValue 2 -NewValue 20

This function takes an array and replaces all occurrences of $OldValue with $NewValue.

Conclusion

Replacing values in an array is a common task in PowerShell scripting. Whether you’re using index assignment, the -replace operator, loops, the ForEach-Object cmdlet, or a custom function, PowerShell provides the flexibility to update your arrays effectively.

In this PowerShell tutorial, I have explained the below different ways to replace values in an array in PowerShell with examples.

  1. Using Index Assignment
  2. Using the -replace Operator
  3. Using a Loop to Replace Values
  4. Using the ForEach-Object Cmdlet
  5. Using a Custom Function

You may also like: