Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell to extract specific column from csv and store in a variable

Tags:

powershell

My keys.csv file looks like this

 PrjKey BldKey      key
 LS     LOOKUPSNAP1 LS-LOOKUPSNAP1
 LS     LSUH3       LS-LSUH3
 LSPERF LPMDS0      LSPERF-LPMDS0
 LSPERF LSP0        LSPERF-LSP0

I want to extract the values column "key" and store in a variable. I tried this

 Import-Csv .\keys.csv  | select key | ft -hide

the output has a blank line at the top. How can I eliminate it. I only want those 4 values to be "stored in a variable" Yes I don't want to save the output as a csv again.

Can someone help please?

like image 255
VRK Avatar asked Oct 08 '15 16:10

VRK


1 Answers

You had the right idea of using Select-Object to get the one property you want. The two issues you had was that Select-Object key returns and object array with a key property when it looks like you just want string array of keys. Also never use Format-cmdlets when you want to save the data or use it in another form. They destroy objects for the purpose of display data on the screen. So that all being said..

$keys = Import-Csv .\keys.csv | select -ExpandProperty key

Depending on your PowerShell version

$keys = (Import-Csv .\keys.csv).key
like image 80
Matt Avatar answered Sep 21 '22 13:09

Matt