How to Convert CSV to HTML Table in PowerShell?

PowerShell provides an efficient way to convert CSV data into HTML tables if you’re working with data in CSV files and need to present it in an HTML format. In this tutorial, I will explain how to convert CSV to an HTML table in PowerShell using different methods with examples.

To convert a CSV file to an HTML table in PowerShell, you can use the ConvertTo-Html cmdlet. First, import the CSV data using Import-Csv, then pipe the data to ConvertTo-Html specifying the desired properties and title. Finally, save the HTML output to a file using Set-Content. For example:

$data = Import-Csv -Path "C:\MyFolder\data.csv"
$html = $data | ConvertTo-Html -Property FirstName,LastName,City,State -Title "User Data"
$html | Set-Content -Path "C:\MyFolder\data.html"

This method provides a quick and efficient way to transform CSV data into a well-structured HTML table.

Convert CSV to HTML Table in PowerShell

There are different ways to convert CSV to HTML table in PowerShell. Let me show you each method with examples.

Method 1: Using ConvertTo-Html Cmdlet

The ConvertTo-Html cmdlet in PowerShell converts .NET objects into HTML that can be displayed in a web browser.

Syntax

Here is the syntax:

ConvertTo-Html [-InputObject] <PSObject> [-Property <Object[]>] [-As {Table | List}] [-PreContent <String>] [-PostContent <String>] [-Head <String>] [-Body <String>] [-Title <String>] [-CssUri <Uri>] [-Fragment] [<CommonParameters>]

The ConvertTo-Html cmdlet takes objects and converts them into HTML. When combined with Import-Csv, it can read CSV data and format it into an HTML table.

Example

Let’s say you have a CSV file named data.csv with the following content. You can even download the same file.

FirstName,LastName,City,State
John,Doe,New York,NY
Jane,Smith,Los Angeles,CA
Michael,Johnson,Chicago,IL

You can convert this CSV to an HTML table with the following PowerShell script:

$data = Import-Csv -Path "C:\MyFolder\data.csv"
$html = $data | ConvertTo-Html -Property FirstName,LastName,City,State -Title "User Data"
$html | Set-Content -Path "C:\MyFolder\data.html"

This script imports the CSV data, converts it to HTML, and saves it to an HTML file.

The exact output is in the screenshot below.

convert csv to html table in powershell

Check out Create a Table with Headers in PowerShell

Method 2: Custom HTML Formatting

You can manually construct the HTML table if you need more control over the HTML output.

Example

$data = Import-Csv -Path "C:\MyFolder\data.csv"
$html = @"
<html>
<head>
<title>User Data</title>
</head>
<body>
<table border="1">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>City</th>
<th>State</th>
</tr>
"@

foreach ($row in $data) {
    $html += "<tr><td>$($row.FirstName)</td><td>$($row.LastName)</td><td>$($row.City)</td><td>$($row.State)</td></tr>"
}

$html += @"
</table>
</body>
</html>
"@

$html | Set-Content -Path "C:\MyFolder\data.html"

This script constructs the HTML table manually, giving you full control over the HTML structure and styling.

Here is the exact output in the screenshot below:

How to Convert CSV to HTML Table in PowerShell

Check out Export Table to CSV in PowerShell

Method 3: Using Out-File with ConvertTo-Html

Another straightforward method is to pipe the HTML output directly to a file in PowerShell.

Let me show you an example.

Example

Import-Csv -Path "C:\MyFolder\data.csv" | ConvertTo-Html -Property FirstName,LastName,City,State -Title "User Data" | Out-File -FilePath "C:\MyFolder\data.html"

This method simplifies the process by combining the steps into a single line of code.

Conclusion

In this tutorial, I explained how to convert CSV to HTML tables in PowerShell using different methods like using the ConvertTo-Html cmdlet, custom HTML formatting, or the Out-File method, etc.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.