question

phantom2000-5377 avatar image
0 Votes"
phantom2000-5377 asked Chris-1748 commented

Clear csv file content

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-server-powershell
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Some thoughts:

If you open up other csv files (including the $CSVPath file) with Excel , do they get parsed correctly?

If you use notepad to edit the $CSVPath file and save it as C:\temp\result.csv, will Excel open it correctly?

Since you have spaces in the headers, try putting double quotes around the names.


0 Votes 0 ·

it's an encoding problem

please try -Encoding

 Get-Content C:\temp\excelTEst.csv | select -First 1 | Set-Content c:\temp\testy.csv -Encoding Default


0 Votes 0 ·
IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered phantom2000-5377 commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This code works like a charm. Thank you.

0 Votes 0 ·
SimpleSamples avatar image
0 Votes"
SimpleSamples answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.