0
#My script is as below 
$empdata = import-csv Log.csv
    ForEach ($Item in $empdata){
    "FirstName = $($empdata.FirstName)
    LastName = $($empdata.LastName)
    Laptop = $($empdata.Laptop)
    AccessCard = $($empdata.AccessCard)
    Phone = $($empdata.Phone)
    Building = $($empdata.Building)
    Location = $($empdata.Location)"
    Write-host $FirstName $LastName $Laptop  $AccessCard $Phone $Building 
    $Location

I want to read data of each line into an array and pass it on to another tool, but unable to do so since I am getting data of all columns to together example all data of column 1 appears..

Results are as below

FirstName = Suresh Krishna Shiva
LastName = setty Murthy Reddy
Laptop = Lenovo Lenovo Hp
AccessCard = yes No Yes
Phone = 123456789 45678932 12378945
Building = T4 P1 DT14
Location = Chennai Bangalore Hyderabad
FirstName = Suresh Krishna Shiva
LastName = setty Murthy Reddy
Laptop = Lenovo Lenovo Hp
AccessCard = yes No Yes
Phone = 123456789 45678932 12378945
Building = T4 P1 DT14
Location = Chennai Bangalore Hyderabad
FirstName = Suresh Krishna Shiva
LastName = setty Murthy Reddy
Laptop = Lenovo Lenovo Hp
AccessCard = yes No Yes
Phone = 123456789 45678932 12378945
Building = T4 P1 DT14
Location = Chennai Bangalore Hyderabad

I  am trying to get data in the below format


FirstName = Suresh
LastName = setty
Laptop = Lenovo
AccessCard = yes
Phone = 123456789
Building = T4
Location = Chennai    
4
  • 2
    You have your pipeline object as $Item but are not using it in the foreach block Commented Sep 18, 2017 at 19:28
  • We do not know what is in your CSV file. Please click here and edit your question to include a sample. Commented Sep 18, 2017 at 19:35
  • Thanks Matt got it..changed code to FirstName = $($ID.FirstName) Commented Sep 18, 2017 at 19:35
  • 1
    Note that you get nearly identical output with Import-Csv Log.csv | Format-List. Commented Sep 18, 2017 at 19:58

3 Answers 3

1
#classiq method
import-csv "c:\temp\test.csv" | %{
   "FirstName=$($_.FirstName)"
   "LastName=$($_.LastName)"
   "Phone=$($_.Phone)"
}

#other method with format
import-csv "c:\temp\test.csv" | %{
   "FirstName={0}`nLastName={1}`nPhone={2}" -f $_.FirstName, $_.LastName, $_.Phone
}
Sign up to request clarification or add additional context in comments.

Comments

0
$empdata = Import-Csv Log.csv

foreach ($item in $empdata) {
    "FirstName = $($item.FirstName)
    LastName = $($item.LastName)
    Laptop = $($item.Laptop)
    AccessCard = $($item.AccessCard)
    Phone = $($item.Phone)
    Building = $($item.Building)
    Location = $($item.Location)"
}

$empdata.FirstName = the whole FirstName column (all rows)

$item.FirstName = the FirstName value for the current row in your foreach iteration

$FirstName $LastName $Laptop $AccessCard $Phone $Building $Location: these are all null in your example (and not needed to produce the output you want)

3 Comments

what I would like to get within this foreach loop is each row, not column. How can I get each row ($item) as a single object that I can add properties to?
@IfediOkonkwo $item is the row object. You should be able to add properties using Add-Member like: $item | Add-Member -MemberType NoteProperty -Name NewField -Value "duh" -Force
Hmmm,, I see. What I'd tried was: $item.NewField = "duh". That didn't work. I guess I'll try Add-Member when I have a moment. Thanks.
0
Import-Csv .\Log.csv | ForEach-Object{

    "
        FirstName= $($_.FirstName) `n
        LastName= $($_.LastName)   `n
        Laptop= $($_.Laptop)       `n
        AcessCard= $($_.AcessCard) `n
        Phone= $($_.Phone)         `n
        Building= $($_.Building)   `n
        Location= $($_.Location)
    "
}

1 Comment

While this code may provide a solution to problem, it is highly recommended that you provide additional context regarding why and/or how this code answers the question. Code only answers typically become useless in the long-run because future viewers experiencing similar problems cannot understand the reasoning behind the solution.

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.