'How to format output of Export-Excel in order to get colored header in PowerShell?

The code below is working fine to export data, but I need the header / first row to have custom background and text colors. I would like the header to be filled with yellow with blue text.

$csvFile = "C:\temp\filename.csv"

$excelfile = "C;\temp\Excelfile.xlsx"

$data = Import-Csv -Path $csvFile 
$data | Export-Excel $excelfile -AutoSize -AutoFilter

I tried a few parameters for Export-Excel but none of them worked and I got the following error:

parameter Not found

Thanks!

Sam



Solution 1:[1]

The following works for me:

$csvFile = "C:\temp\filename.csv"

$excelfile = "C:\temp\Excelfile.xlsx"

$data = Import-Csv -Path $csvFile 

$ConditionalFormat =$(
    New-ConditionalText -Range 'A1' -BackgroundColor Yellow -ConditionalTextColor Blue
)
$data | Export-Excel $excelfile -ConditionalFormat $ConditionalFormat -show

I added the -show at the end to auto open the $excelfile, but it is not necessary.

(I realize this post is ancient, just wanted to throw it out there for anyone else)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ScarlettRogue