r/PowerShell • • 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.

3 Upvotes

17 comments sorted by

View all comments

Show parent comment

2

u/BlackV Apr 26 '26 edited Apr 27 '26

EDIT: OH 3x~~~ is a code fence on new.reddit, not only 3 backticks ``` TIL

Updated reply

2

u/BlackV Apr 26 '26 edited Apr 27 '26

Now there’s several ways in ps to get at a particular property in a record set:

  1. The traditional way. From the list, you select a particular record using an index, then you select the property of the object at that index.

     powershell $csv[$index].property
  2. But if you think of your recordset as a simple image of your 2d matrix that is csv, in powershell, not only can you select rows but you can also select columns.

     powershell $csv.property

Elsewhere this would be an error. The list has no property by that name.

Ps will transparently unroll the list and put any value of the named property into the pipeline, as a flattened list. That’s basically powershell magic.