none
Read csv file line by line and store output to another csv file with multiple columns using PowerShell RRS feed

  • Question

  • Hi There,

    I am working with csv file using PowerShell. I have a file and I want to search it with some condition line by line. I am able to do it but I want multiple columns from that csv file with headers. Can someone please help me here?

    Example, My csv looks like below

    Identity,Email,Office,SMTP
    apujari,apujari@difference.com,Pune,None
    abhosale,abhosale@difference.com,Pune,None

    I am using below and storing output with one column without header

    $path = "c:\users\apujari\documents"

    foreach ($line in (Import-Csv "$path\Test.csv"))
    {
      # Check if 'ForwardingSmtpAddress' value in the line is not @shipco.com.
      if ($line.SMTP -eq 'None')

      {
       #If ForwardingSmtpAddress is none of the above then store output in csv file

      $line.Identity | Out-File -FilePath "$path\Test4.csv" -Append
                      
       }
    }

    I want 2 columns with header in out-file.

    Please someone help me here.


    Abhay

    Wednesday, September 26, 2018 12:30 PM

Answers

  • If you would read the help and take time to learn PowerShell at least at a basic level these things would be bery easyu.

    $path = 'c:\users\apujari\documents'
    
    Import-Csv $path\Test.csv | 
        Where-Object{$_.SMTP -eq 'None'} |
    Select-Object Identity, SMTP | Export-Csv $path\Test4.csv -NoTypeInformation


    \_(ツ)_/


    • Edited by jrvModerator Wednesday, September 26, 2018 1:12 PM
    • Marked as answer by abhvp Wednesday, September 26, 2018 1:33 PM
    Wednesday, September 26, 2018 1:12 PM
    Moderator

All replies

  • Use "Export-Csv" to create a file with columns and a header.

    help export-csv -online

    Always start by reading the help before asking a question that has an answer documented in the help.


    \_(ツ)_/

    Wednesday, September 26, 2018 12:46 PM
    Moderator
  • Hi,

    I am afraid that I am unable to understand you. Could you please elaborate? I just want 2 columns with headers in output file.


    Abhay

    Wednesday, September 26, 2018 1:03 PM
  • Did you read the help I posted above?  Start by carefully reading how to use the command and look over all of the examples.


    \_(ツ)_/

    Wednesday, September 26, 2018 1:05 PM
    Moderator
  • Does this help?

    $path = "c:\users\apujari\documents"
    "Header1,Header2" | out-file -FilePath "$path\Test4.csv"
    foreach ($line in (Import-Csv "$path\Test.csv"))
    {
      # Check if 'ForwardingSmtpAddress' value in the line is not @shipco.com.
      if ($line.SMTP -eq 'None')
      {
       #If ForwardingSmtpAddress is none of the above then store output in csv file
      "$($line.Identity),$($line.smtp)" | Out-File -FilePath "$path\Test4.csv" -Append
                      
       }
    }

    Wednesday, September 26, 2018 1:07 PM
  • No. Did you read the help yet?


    \_(ツ)_/

    Wednesday, September 26, 2018 1:08 PM
    Moderator
  • If you would read the help and take time to learn PowerShell at least at a basic level these things would be bery easyu.

    $path = 'c:\users\apujari\documents'
    
    Import-Csv $path\Test.csv | 
        Where-Object{$_.SMTP -eq 'None'} |
    Select-Object Identity, SMTP | Export-Csv $path\Test4.csv -NoTypeInformation


    \_(ツ)_/


    • Edited by jrvModerator Wednesday, September 26, 2018 1:12 PM
    • Marked as answer by abhvp Wednesday, September 26, 2018 1:33 PM
    Wednesday, September 26, 2018 1:12 PM
    Moderator
  • Did you read the help I posted above?  Start by carefully reading how to use the command and look over all of the examples.


    \_(ツ)_/

    Hi,

    I have gone through export-csv but I am not sure how I can use this in my case to achieve expected result.

    I am searching csv file for search string and want output in another csv file with 2 columns (with headers). It will be helpful if you can provide some example.


    Abhay

    Wednesday, September 26, 2018 1:16 PM
  • If you would read the help and take time to learn PowerShell at least at a basic level these things would be bery easyu.

    $path = 'c:\users\apujari\documents'
    
    Import-Csv $path\Test.csv | 
        Where-Object{$_.SMTP -eq 'None'} |
    Select-Object Identity, SMTP | Export-Csv $path\Test4.csv -NoTypeInformation


    \_(ツ)_/



    Thank you. This has cleared my confusion. I was trying to read csv file line by line. I am still learning PowerShell and every task teaching me something new.

    Abhay

    Wednesday, September 26, 2018 1:34 PM
  • You have been doing this for a year now and you are not any further than when you started.

    In order to use any technology it is necessary to learn the basics formally.  Technology cannot be learned by guesswork.

    I suggest starting here "Learn PowerShell" and do the whole thing until you understand the basics. After that guessing will be helpful.


    \_(ツ)_/

    Wednesday, September 26, 2018 1:38 PM
    Moderator