PowerShell ConvertTo-Html vs Export-Csv: Choose Output Format

PowerShell ConvertTo-Html vs Export-Csv: Choose Output Format

PowerShell Tips Editor 1 min read
PowerShell ConvertTo-Html vs Export-Csv: Choose Output Format

You have a collection of server objects in PowerShell and need to share them — but the right format depends entirely on who will consume the data. Executives want a visual HTML table in their inbox. Analysts want a CSV they can open in Excel. Developers consuming an API want JSON. This guide compares every PowerShell PowerShell output format HTML CSV option so you choose the right format for each audience without trial and error.

Export-Csv for Spreadsheet Consumers

Export-Csv serializes objects into a flat CSV file where each property becomes a column. It is the best choice when consumers will open the data in Excel, import it into a database, or process it in another script:

$data = Get-Service | Select-Object Name, DisplayName, Status, StartType

# Always use -NoTypeInformation to suppress the #TYPE header line
$data | Export-Csv -Path "C:\Reports\services.csv" -NoTypeInformation

# For international compatibility, specify UTF8
$data | Export-Csv -Path "C:\Reports\services.csv" -NoTypeInformation -Encoding UTF8

# Round-trip: read back as objects
$imported = Import-Csv "C:\Reports\services.csv"
$imported.Count
231

ConvertTo-Html for Email Reports

ConvertTo-Html is ideal when you want to email a formatted table or embed a report in a web page. Add a CSS -Head block for professional styling:

$css = '<style>body{font-family:Segoe UI,sans-serif} table{border-collapse:collapse;width:100%} th{background:#0078d4;color:white;padding:8px} td{padding:6px;border-bottom:1px solid #ddd}</style>'

$html = Get-Service | Where-Object Status -eq Stopped |
    Select-Object Name, DisplayName, Status |
    ConvertTo-Html -Head $css -PreContent "<h2>Stopped Services</h2>"

$html | Out-File "C:\Reports\stopped-services.html" -Encoding UTF8
Send-MailMessage -Body ($html | Out-String) -BodyAsHtml -SmtpServer "smtp.corp.com" `
    -From "[email protected]" -To "[email protected]" -Subject "Stopped Services Report"

Out-GridView for Interactive Exploration

Out-GridView displays data in an interactive, filterable, sortable grid window. It is a quick way to explore data during a troubleshooting session, not a production output format:

# Interactive exploration — opens a GUI window
Get-Process | Select-Object Name, CPU, WorkingSet64, Id | Out-GridView -Title "Running Processes"

# Use -PassThru to allow selection and return selected items
$selectedProcesses = Get-Process | Out-GridView -Title "Select processes to kill" -PassThru
$selectedProcesses | Stop-Process -WhatIf

# Note: Not available in non-GUI sessions (SSH, headless servers)

ConvertTo-Json for API Consumers

ConvertTo-Json serializes objects to JSON, which is the standard exchange format for REST APIs, configuration files, and JavaScript frontends. Use -Depth to control how deeply nested objects are serialized:

$data = Get-Process | Select-Object -First 5 Name, Id, CPU, WorkingSet64

# Default depth is 2 — increase for deeply nested objects
$json = $data | ConvertTo-Json -Depth 3
$json | Out-File "C:\Data\processes.json" -Encoding UTF8

# Compact JSON (no whitespace) for API payloads
$compactJson = $data | ConvertTo-Json -Compress

# Round-trip
$fromJson = $json | ConvertFrom-Json
$fromJson[0].Name

Format-Table and Format-List for Console

Format-Table and Format-List produce human-readable console output. They are only for display — objects processed by Format-* cmdlets cannot be further piped to data cmdlets:

Get-Service | Format-Table Name, Status, StartType -AutoSize

# Detailed view for one item
Get-Service Spooler | Format-List *

# Format-Wide for a simple list
Get-Service | Where-Object Status -eq Running | Format-Wide Name -Column 4

# WRONG — Format-* must be LAST in a pipeline
# This returns formatting objects, not service objects:
Get-Service | Format-Table | Export-Csv test.csv   # BAD

# CORRECT:
Get-Service | Export-Csv test.csv -NoTypeInformation  # GOOD

Decision Guide

Choose your output format based on the consumer:

  • Excel / spreadsheet consumer: Export-Csv -NoTypeInformation
  • Email recipient / web report: ConvertTo-Html with CSS styling
  • REST API / JavaScript consumer: ConvertTo-Json -Depth N
  • Interactive troubleshooting: Out-GridView (GUI sessions only)
  • Console display only: Format-Table or Format-List (must be last in pipeline)
  • Another PowerShell script: Export-Clixml to preserve full type fidelity

Common Errors and Fixes

  • Format-* cmdlets break the pipeline — always last in chain. Once data passes through Format-Table or Format-List, it becomes formatting instructions, not objects. You cannot pipe formatting output to Export-Csv, Where-Object, or any data cmdlet. Apply formatting only at the very end of a pipeline, only when the goal is console display.
  • Out-GridView not available in non-GUI sessions. Running Out-GridView over SSH, in a scheduled task, or in a PowerShell Remoting session throws an error because there is no GUI to display the window. Use Out-GridView only in interactive desktop sessions.

Related Cmdlets / See Also

Wrapping Up

Match your output format to your audience: CSV for spreadsheet users, HTML for email recipients, JSON for APIs, and Format-Table only for console display as the final step in a pipeline. Never mix Format-* cmdlets with data processing, and remember that Out-GridView requires a GUI session.

Send-Item -To