Clear csv file content

Arosh Wijepala 161 Reputation points
2021-05-25T13:52:03.673+00:00

Hi,

I'm using below code to clear .csv file leaving the headers. But when the code is run, I get the headers in the same cell when I open it in Excel. Please see below screen.

Code:

 If($DateDifference -gt 20)
        {
            (Get-Content $CSVPath |  Select-Object -First 1) | Out-File "C:\temp\result.csv"

        }

Result: https://prnt.sc/13ecdpx

How can I remove only the rows and keep the headers intact?

Thanks in advance.

Windows for business | Windows Server | User experience | PowerShell
{count} votes

Accepted answer
  1. Anonymous
    2021-05-26T01:44:58.217+00:00

    Hi,

    You could try some other delimiters. The Tab character works for me.

    (Get-Content $CSVPath |  Select-Object -First 1).replace(",","`t")  | Out-File "C:\temp\result.csv"  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. Sam of Simple Samples 5,546 Reputation points
    2021-05-25T18:04:45.29+00:00

    Are you sure you need the parentheses? All of the following work for me.

    Get-Content $CSVPath -Head 1 > "C:\temp\result.csv"
    Get-Content $CSVPath | select -First 1 > "C:\temp\result.csv"
    Get-Content $CSVPath -TotalCount 1 > "C:\temp\result.csv"
    

    If none of those work for you then update your question and add something showing the contents of C:\temp\result.csv using a text editor such as Notepad, not using Excel.

    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2021-05-26T01:51:25.07+00:00

    Keeping in mind that CSV files may have comment lines preceding the header row, this code gets enough of the CSV file (15 lines) to include the header row and then converts the data to PSCustomObjects (which ignores the comment lines). The first (or any, really) object is then used to extract the column names which are then quoted and joined with commas.

    $infile =  'C:\junk\x2.csv'
    $outfile = 'C:\junk\x2-1.csv'
    $hdr = @()
    # Get enough of the CSV to include the header if there are leading
    # comment lines in the file
    $x = Get-Content $infile -First 15 | ConvertFrom-CSV
    $x[0].psobject.Members |    # get the property names from the 1st item (ignore the rest)
        ForEach-Object{
            if ($_.MemberType -eq 'NoteProperty'){
                $hdr += '"' + $_.Name + '"' # surround the names with quotes (just in case!)
            }
        }
    $hdr -join ',' | Out-File $outfile  # join names with commas and write the header
    

    Here's an example of a commented CSV:
    # a comment line
    # another comment line
    "User","Address","Another piece of data"
    "User1","Street1 City1",A
    "User2","Street2 City2",B
    "User3","Street3 City3",C

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.