r/PowerShell • u/Zealous_Automation • Apr 26 '26
Changing values of elements in an array
I just figured out how to reference individual elements in an array. But I can't seem to modify the element values
I start off with a $data = import-csv -path "path\to\file.csv"
Then do some loops. For debugging, I'm outputting the values in the part of the loop where I'm modifying values:
$data.OffLastName[$jj]
$data.ReportsToLast[$jj]
$data.managerid[$jj]
$Manager = "Manager"
$Manager
$data.ManagerID[$jj] = $Manager
$data.ManagerID[$jj]
And the output looks like this:
Brown
Young
{null} (I've also tested this as "XXXXXXXXX")
Manager
{null} (This resulted in XXXXXXXXX during my other test)
Based off my test code, I want the last {null} value to reflect "Manager" and I can't figure out what I'm doing wrong. I've also attempted to fill the values on the .csv with text to see if it was a string vs number mismatch on variable type.
I could use some help because I feel like its something obvious. But according to google searches, it seems like I'm doing it right.
10
u/purplemonkeymad Apr 26 '26
Selecting a property in an array ( using . ) actually creates a new temporary array to create the list of property values ie:
creates a new array with only the values of the ReportsToLast property. This means that using a index selector on the array ( [] ) is dependant on where you put it.
So:
and
have the same value, but they point to different points in memory.
If you are accessing a property of an array, access the index of the array first:
2
u/thomsxD Apr 26 '26
This would be so much easier with full context...
1
u/Th3Sh4d0wKn0ws Apr 26 '26
$csv would be an array of pscustomobjects, but I'm not getting why you're calling properties from that array and then indexing into that property with $jj.
what is $jj?
1
u/Trakeen Apr 26 '26
Some things in powershell (especially when accessing .net types directly) can be case sensitive. Have you validated managerid and ManagerID are the same object/property?
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_case-sensitivity?view=powershell-7.6
3
u/surfingoldelephant Apr 27 '26
This is called member-access enumeration (assuming
$datais a collection).You aren't making a change to the element at index
$jjin$data. You're making a change to the array ofManagerIDvalues created by member-access enumeration. Hence why there doesn't appear to be any change in$dataafter the value assignment.The assignment is succeeding, it's just not being applied to a
$dataelement. You can see this more clearly with:Instead, you want to operate directly on
$datalike u/purplemonkeymad showed or rework how you're enumerating$datato begin with.In general, you shouldn't assume the object count produced by member-access enumeration is the same as the original collection's count. There are lots of reasons why, including bugs in its implementation.
1
u/vadertator22 Apr 27 '26
You have to use index then find what your wanting to change and set the value. I have done this before then after changing whether I am looking for I export results. For example I had a computer name data that also sometimes had fqdn for name so those with fqdn I would change the value in memory to short name so they all matched.
0
u/DutchDallas May 01 '26
Suggestion for you: open copilot and paste your script in there and prompt it to review the code so that it matches your expected outcome (paste that too). You'll realize how helpful AI can be, albeit not perfect.
If you're still stumped, this group is a great help.