neildeadman
neildeadman

Reputation: 3969

Convert CSV to Excel file in PowerShell

I'm using the following to save a CSV file of output from Event Logs on several Windows Servers:

    Get-EventLog -LogName $logName -ComputerName $servers -Newest 2 -Entry 'Error', 'Warning' -ErrorAction SilentlyContinue | Sort MachineName, TimeWritten | Select MachineName, Source, TimeWritten, EventID, EntryType, Message | Export-CSV $csvFile #ConvertTo-CSV #Format-Table -Wrap -Property Source, TimeWritten, EventID, EntryType, Message -Autosize | 

I then remove the first line (as it starts with #Type and is unneeded!):

$file = Get-Content $csvFile 
$file[1..($file.length-1)] | Out-File $csvFile -Encoding ascii

When opened in Excel, it isn't formatted very nice. I'd like to convert this to Excel and format the cells to fit contents, change font size.

Can this be done from within my script?

Upvotes: 0

Views: 2995

Answers (1)

manojlds
manojlds

Reputation: 301037

When you are calling export-csv, add the -NoTypeInformation flag and that will remove the line type information line.

Try that, and as such, the csv file should be properly opened in excel and you won't need to do anything more.

To do more formatting like fit contents and change font size will be more work. You will have to use COM and do some manipulation.

Have a look here for some examples and further guidance - http://www.petri.co.il/export-to-excel-with-powershell.htm

Upvotes: 1

Related Questions