Working with CSV, JSON, and XML
Learn to import, process, export, and transform data in CSV, JSON, and XML formats using PowerShell cmdlets and techniques.
📖 4 min read📅 2026-02-10Working with Data
Working with CSV
CSV (Comma-Separated Values) is one of the most common data formats.
Importing CSV
# Import a CSV file
$employees = Import-Csv "employees.csv"
# Access data (each row is an object)
$employees[0].Name
$employees[0].Department
$employees | Where-Object { $_.Department -eq "IT" }
# Import with custom delimiter
$data = Import-Csv "data.tsv" -Delimiter "`t"
# Import with custom headers
$data = Import-Csv "noheader.csv" -Header "Name", "Age", "City"Creating CSV Data
# From objects
$people = @(
[PSCustomObject]@{ Name = "Alice"; Age = 30; City = "NYC" }
[PSCustomObject]@{ Name = "Bob"; Age = 25; City = "LA" }
[PSCustomObject]@{ Name = "Charlie"; Age = 35; City = "Chicago" }
)
$people | Export-Csv "people.csv" -NoTypeInformation
# Append to existing CSV
[PSCustomObject]@{ Name = "Diana"; Age = 28; City = "Seattle" } |
Export-Csv "people.csv" -Append -NoTypeInformation
# Convert to CSV string (not to file)
$people | ConvertTo-Csv -NoTypeInformationProcessing CSV Data
$sales = Import-Csv "sales.csv"
# Filter
$highSales = $sales | Where-Object { [decimal]$_.Amount -gt 1000 }
# Group and summarize
$sales | Group-Object Region | ForEach-Object {
[PSCustomObject]@{
Region = $_.Name
Count = $_.Count
TotalSales = ($_.Group | Measure-Object Amount -Sum).Sum
}
}
# Sort
$sales | Sort-Object { [decimal]$_.Amount } -Descending
# Add calculated column
$sales | Select-Object *, @{
Name = "Tax"
Expression = { [math]::Round([decimal]$_.Amount * 0.1, 2) }
}Working with JSON
JSON is the standard for APIs and modern config files.
Importing JSON
# From file
$config = Get-Content "config.json" -Raw | ConvertFrom-Json
# Access properties
$config.database.host
$config.database.port
$config.features[0]
# From API
$response = Invoke-RestMethod "https://jsonplaceholder.typicode.com/users"
$response | Select-Object name, email, phoneCreating JSON
# From hashtable
$data = @{
name = "ShellRAG"
version = "1.0"
features = @("PowerShell", "Bash", "Automation")
database = @{
host = "localhost"
port = 5432
}
}
$data | ConvertTo-Json -Depth 5 | Out-File "config.json"
# From objects
Get-Process | Select-Object Name, CPU, WorkingSet -First 5 |
ConvertTo-Json |
Out-File "processes.json"
# Pretty-print with depth
$nested = @{
Level1 = @{
Level2 = @{
Level3 = "deep value"
}
}
}
$nested | ConvertTo-Json -Depth 10Modifying JSON
# Read, modify, write
$config = Get-Content "config.json" -Raw | ConvertFrom-Json
# Add or update properties
$config.version = "2.0"
$config | Add-Member -NotePropertyName "newFeature" -NotePropertyValue $true -Force
# Write back
$config | ConvertTo-Json -Depth 5 | Set-Content "config.json"Working with XML
XML is still common in enterprise systems and configuration files.
Reading XML
# Load XML file
[xml]$xml = Get-Content "data.xml"
# Or use XmlDocument
$xml = New-Object System.Xml.XmlDocument
$xml.Load("data.xml")
# Navigate XML
$xml.catalog.book[0].title
$xml.catalog.book | Where-Object { $_.genre -eq "Computer" }
# Using Select-Xml with XPath
$books = Select-Xml -Path "data.xml" -XPath "//book[@genre='Computer']"
$books.Node.titleCreating XML
# Using here-string
$xmlContent = @"
<?xml version="1.0" encoding="UTF-8"?>
<servers>
<server name="web01" ip="192.168.1.10" role="web" />
<server name="db01" ip="192.168.1.20" role="database" />
<server name="app01" ip="192.168.1.30" role="application" />
</servers>
"@
$xmlContent | Out-File "servers.xml"
# Using XmlWriter
$writer = [System.Xml.XmlWriter]::Create("output.xml")
$writer.WriteStartDocument()
$writer.WriteStartElement("config")
$writer.WriteElementString("name", "MyApp")
$writer.WriteElementString("version", "1.0")
$writer.WriteEndElement()
$writer.WriteEndDocument()
$writer.Close()Converting Between Formats
# CSV to JSON
Import-Csv "data.csv" | ConvertTo-Json | Out-File "data.json"
# JSON to CSV
(Get-Content "data.json" -Raw | ConvertFrom-Json) |
Export-Csv "data.csv" -NoTypeInformation
# Objects to HTML report
Get-Process | Select-Object Name, CPU, WorkingSet -First 20 |
ConvertTo-Html -Title "Process Report" -CssUri "style.css" |
Out-File "report.html"Practical Example: API Data Processing
# Fetch users from API, process, and export
$users = Invoke-RestMethod "https://jsonplaceholder.typicode.com/users"
# Process and reshape data
$report = $users | ForEach-Object {
[PSCustomObject]@{
Name = $_.name
Email = $_.email
City = $_.address.city
Company = $_.company.name
Website = $_.website
}
}
# Export in multiple formats
$report | Export-Csv "users.csv" -NoTypeInformation
$report | ConvertTo-Json | Out-File "users.json"
$report | ConvertTo-Html -Title "User Report" | Out-File "users.html"
Write-Host "Exported $($report.Count) users to CSV, JSON, and HTML"Exercises
- Create a CSV with 10 products (Name, Price, Category, Stock) and write a script to generate a summary report
- Build a script that reads a JSON config file, modifies settings, and writes it back
- Convert an XML configuration file to JSON format
- Create a script that fetches data from a public API and exports it to CSV
- Build a report generator that combines data from multiple CSV files
Next: Modules and Script Organization — learn to package and share code!