Vader55
Vader55

Reputation: 21

Extracting particular columns from a json converted csv file using powershell, or from the initial json file itself

I had been given a simple Json file which had to be converted to csv. I did so using powershell. The converted csv file has 5 columns out of which I need to extract the 1st and the 5th and then print it in the powershell console and assign it to a variable of a shell code via the batch file.

These are the Csv columns: CSV columns imported from json

Only the headers 'name' and 'last_modified' have to be extracted and assigned to a variable or only printed in the powershell console.

This is the shell command in batch file (cygwin command) in which the column values (via a new csv or directly) have to be assigned to the %2 part.

IF in case this is not possible, just the 'name' and the 'last_modified' keys directly from json have to be assigned to %2 part of the cygwin code.

I have tried importing the csv in powershell and assigning it to $A which shows this output

I have tried using this command as well,

PS D:\> $A | Get-Member name, last_modified -View Extended

but it only shows the first 'name' and the first 'last_modified' value from the csv.

My apologies if this is a very basic question, I am completely new to powershell and batch programming.

Upvotes: 2

Views: 232

Answers (1)

JonasP
JonasP

Reputation: 21

Get-Member will return only 1 row, you should be using Select-Object as Theo points out in his comment.

Try this to see the difference

$csv = Get-Service

$csv | Get-Member Status, Name #Returns 2 columns but only 1 row

$csv | Select-Object Status, Name #Returns 2 columns and all the rows

Upvotes: 1

Related Questions