6

The powershell script:

$test = import-csv “C:\CSVFiles\test.csv”
ForEach ($item in $test)
{
$Name = $item.(“Name”) 
$property = $item.("property")
$location = $item.(“location”)

Write-Output "$Name=$Name"
Write-Output "Property=$property"
Write-Output "Location=$location"
}

This script shows all the data for name,property and location for each row. I want the results to only show the data of one row;for example the row: n1;13;computer

The Cvs file =

Name;property;location
n1;13;computer
n2;65;computer
n3;12;tablet
n4;234;phone
n5;123;phone
n6;125;phone

What the current script spits out:

Name=n1
Property=13
Location=computer
Name=n2
Property=65
Location=computer
Name= n3
Property=12
Location=tablet
Name=n4
Property=234
Location=phone
Name=n5
Property=123
Location=phone
Name=n6
Property=125
Location=phone
4
  • 1
    Don't use a ForEach then. Use $test[0] or whatever index you need. Commented Jun 14, 2018 at 8:29
  • Do you want to list x-th entry or filter that based on property value? Also, you can skip assigning values to variables and use Write-Output "Name=$($item.Name)" Commented Jun 14, 2018 at 8:33
  • @Lieven sorry i did not notice your comment, i am new around here :). I am not familliar with where i would put this $test[0] to in the script? I tried to put it on the place of ForEach. Commented Jun 14, 2018 at 9:17
  • @Mark - LotPings' answer is pretty comprehensive. Better than what I can put in comments. Commented Jun 14, 2018 at 9:42

2 Answers 2

10

There are many ways to select a Row of a csv and to present the data

For demonstration I use an inline csv with a here string.

$Test = @"
Name;property;location
n1;13;computer
n2;65;computer
n3;12;tablet
n4;234;phone
n5;123;phone
n6;125;phone
"@ | ConvertFrom-Csv -delimiter ';'

> $test[0]

Name property location
---- -------- --------
n1   13       computer

> $test | where-object Name -eq 'n1'

Name property location
---- -------- --------
n1   13       computer

> $test | where-object Name -eq 'n1' | Select-Object Name,property

Name property
---- --------
n1   13

> $test | where-object Name -eq 'n1' | ForEach-Object {"Name:{0} has property: {1}" -f $_.Name,$_.property}
Name:n1 has property: 13

Once imported the csv rows contents are converted to objects

If you want to get the original row of the csv matching a criteria don't import but:

> Get-Content "C:\CSVFiles\test.csv" | Select-String '^n1'

n1;13;computer

^n1 is a regular expression anchoring the pattern at line begin.

Select-String -Path "C:\CSVFiles\test.csv" -Pattern '^n1'

Is the same without a pipe

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

Comments

0

So your current output is having 3 objects that are having 3 headers majorly. One is Name; Second one is Property and the third one is Location.

As part of solution, you can either pull the records by specifying the index value or you can use the .Headername to pull all the sets of same object. Like:

Avoiding Foreach and accessing with $test[0] or $test[1]

Or you can use like: $test.Name directly to have all of the names from the csv

2 Comments

Okey Ranadip thank you so much for answering! How would you use the "$test[0] or $test[1]" -option here, if the values of only "name" and "property" are needed from the first row (n1;13;computer) ?
Just use this: $test[0] | Select Name, Property

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.