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,ILYou 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.

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:

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:
- Create an HTML Table from Variables in PowerShell
- Convert String to HTML Table in PowerShell
- Get the First and Last Line of a CSV File in PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.