2

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.

2
  • 2
    Please don't post screenshots of code - cut and paste the text into your question instead and format it with "code fences" - i.e. a line containing three backticks before and after the code. This will make it easier for other people to try to reproduce your issue, and you'll be more likely to get an answer as a result. Commented May 10, 2022 at 8:22
  • Get-Member name, last_modified --> Select-Object name, last_modified Commented May 10, 2022 at 9:27

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot! 'Select-Object' worked! Now all that's left is to transfer these values to the %2 variable from the cygwin command in the batch file.
I'm glad it worked! Dont forget to set the answer as correct so future users with this issue know it has been solved :)

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.