1

I have some code that pulls from an API into a variable called $table. I can export this variable to a csv with no issues, except the last field in the table ($table.userdefinedfields) exports as a SystemObject[].

What I would like is a 3 column table consisting of the 1st and 3rd fields of $table and the 29th field of the SystemObject[].

I am not too well versed in powershell, but this must be possible.

Any ideas?

2
  • 3
    It's possible. Could you edit your post to include the original result of the API call (sanitizing values as needed)? Commented Nov 24, 2021 at 17:08
  • 1
    Actually SystemObject[] means Array of Elements, CSV file can show only single string elements, the fast and easy solution is to join the Array elements with a comma (or any other character), for example $table.userdefinedfields -join "," and then export it again, if you need more precise solution, add your code to the question Commented Nov 24, 2021 at 17:17

1 Answer 1

2

Use Select-Object with a calculated property:

$table | 
  Select-Object -ExcludeProperty userdefinedfields -Property *, @{
    Name = 'Field29' # change as needed
    Expression = { $_.userdefinedfields[28] }
  } # | Export-Csv ...

The above uses all original properties (*), except the userdefinedfields property, and adds a calculated property with self-chosen name Field29 that contains the 29th element ([28]) of the array stored in each input object's .userdefinedfields property.

Since you only want a subset of the original properties, replace * with the property names of interest, i.e the names of the 1st and 3rd properties (e.g., Foo, Bar); you can then omit the -ExcludeProperty parameter.

Sign up to request clarification or add additional context in comments.

1 Comment

Glad to hear it, @TomRepetti; my pleasure. Please allow me to give you the standard advice to newcomers in the next comment; please also consider revisiting your previous questions:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.