1

I have imported a csv file

$infile = import-csv .\myfile.csv

Now I can show the columns (now object properties) by

$infile | gm -type NoteProperty

Name      MemberType

-------   ----------

Column1   NoteProperty

Column2   NoteProperty

etc...

What I want to do is get the value of the Name(Column1..) into a variable.

4 Answers 4

7

You can use select

$infile | gm -type NoteProperty | select -ExpandProperty Name

e.g.,

$Names = @( $infile | gm -type NoteProperty | select -ExpandProperty Name )
Sign up to request clarification or add additional context in comments.

1 Comment

$infile | Get-Member -Type NoteProperty | Select-Object -ExpandProperty Name to avoid using aliases :-)
2

Another way would be to go through PSObject:

$infile[0].PSObject.Properties |
  ? { $_.MemberType -eq 'NoteProperty' } |
  select -Expand Name

Using $infile[0] selects the first object from the collection, otherwise .PSObject.Properties would return the properties of the collection instead of the properties of a collection element.

Comments

0

This should print Column1:

$infile = import-csv .\myfile.csv
$gm = $infile | gm -type NoteProperty
$name = $gm[0].Name
$name

Comments

0

When importing cdv files using powershell, I normally specify a delimiter incase of more than a single value. Hope this helps all.

Example csv file

 Key     ValueA     ValueB
 ref1    valueA1    valueB1
 ref2    valueA2    valueB2

When opening a csv file using notepad

 Key;ValueA;ValueB
 ref1;valueA1;valueB1
 ref2;valueA2;valueB2

Using powershell to import that csv file into a variable

 $Hash = Import-Csv -path d:\test.csv -Delimiter ';'

Using powershell to query the csv file

 $Hash = Import-Csv -path d:\test.csv -Delimiter ';' |? {$_.ValueA -eq 'x' -and $_.ValueB -eq 'y'} | Select -exp Key

Comments

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.