PowerTip: Change Value of Element in Array with PowerShell


Summary: Learn how to use Windows PowerShell to change the value of an element in an array.

Hey, Scripting Guy! Question How can I use Windows PowerShell to update one of the values in one of the elements of an array?

Hey, Scripting Guy! Answer Use the Item method to retrieve the element and assign a new value. Here is an example of
           updating the value of the first element in an array:

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

$a.Item(0) = 12

$a

Note  Remember that in Windows PowerShell, all arrays are zero-based. This means that the first
element is actually element 0.

Comments (2)

  1. Thanks again for this, but like Thomas pointed, I will appreciate some practical applications

  2. Anonymous says:

    Hello!

    Is there a significant advantage to perform this task like you describe rather than doing the following?

    $a[0] = 12

    Thanks!

Skip to main content