Module 15 Lesson 2 of 6 🕑 ~45 min

> cat module-15-2-data-processing.md

Data Processing: Files, CSV & JSON

Most real-world automation isn't about clever logic — it's about moving data between formats safely. This lesson covers files, the CSV bulk-operations pattern that drives most enterprise IAM automation, and JSON, the format nearly every modern API speaks.

1 Working with Files

List files with Get-ChildItem (aliases dir and ls exist, but production scripts should generally favour the full cmdlet name for readability). Search recursively with Get-ChildItem C:\Logs -Recurse, filter by pattern with Get-ChildItem C:\Logs -Filter "*.log", and find large files with:

Get-ChildItem C:\Logs -File |
Where-Object Length -gt 100MB |
Select-Object Name, Length

Read a text file with Get-Content "C:\Logs\application.log". Search within it with Select-String -Path "C:\Logs\application.log" -Pattern "ERROR" — genuinely useful during incident investigations (Module 14), e.g. Select-String -Path "*.log" -Pattern "authentication failed" across every log file in a directory at once.

2 Reading CSV & Bulk Operations

CSV processing is one of the most common forms of enterprise automation — creating a thousand users, disabling five hundred accounts, updating departments, assigning licences, generating reports. Import-Csv converts CSV rows into objects whose columns become properties. Given users.csv:

FirstName,LastName,Username,Department
Alice,Smith,asmith,Finance
John,Jones,jjones,IT
Sarah,Patel,spatel,Security
$Users = Import-Csv ".\users.csv"
$Users

foreach ($User in $Users) {
    Write-Host $User.Username
}

foreach ($User in $Users) {
    Write-Host "$($User.FirstName) $($User.LastName)"
}

In an Active Directory environment, the pattern conceptually extends to:

$Users = Import-Csv ".\users.csv"

foreach ($User in $Users) {
    Write-Host "Creating $($User.Username)"
}

— and, after validating the data, could be extended with an AD cmdlet like New-ADUser. Never let a script jump straight to bulk production changes. Work through this order every time: import the data, validate the data, display the intended changes, test one record, test several records, use a test OU, enable logging, and only then perform the bulk operation. Every one of Lesson 5's safety rules exists precisely because skipping this order is how one typo becomes a thousand-account incident.

3 Exporting CSV & International Considerations

Reports are exported just as easily:

Get-Service |
Select-Object Name, Status |
Export-Csv ".\services.csv" -NoTypeInformation

A common real-world example — a disabled-accounts report for IAM audits, security assessments, or offboarding investigations:

Get-ADUser -Filter * -Properties Enabled |
Where-Object Enabled -eq $false |
Select-Object Name, UserPrincipalName |
Export-Csv ".\DisabledUsers.csv" -NoTypeInformation

CSV files don't always use commas — some countries and systems use ; as the delimiter instead. PowerShell supports custom delimiters and culture-aware CSV imports: Import-Csv ".\users.csv" -Delimiter ";". This matters particularly in global companies, where data may arrive from systems configured for entirely different regional settings (Module 10, Lesson 5's international-names discussion touches the same underlying issue from the IAM side).

4 JSON

Modern APIs usually return JSON rather than CSV (Module 13). Given:

{
    "username": "asmith",
    "department": "Finance",
    "enabled": true
}

PowerShell converts it into an object:

$Json = Get-Content ".\user.json" -Raw
$User = $Json | ConvertFrom-Json

$User.username
# asmith

And the reverse, converting a PowerShell object into JSON, uses ConvertTo-Json:

$User = @{
    username   = "asmith"
    department = "Finance"
}

$User | ConvertTo-Json

This becomes essential the moment you're calling a REST API — exactly what Lesson 3 covers next.

Lesson Outcome

You should now be able to list/search/filter files and search inside them with Select-String, import a CSV into objects and loop through them safely, follow the import-validate-preview-test-bulk order for destructive CSV-driven changes, export filtered results back to CSV, handle non-comma delimiters, and convert freely between JSON and PowerShell objects with ConvertFrom-Json/ConvertTo-Json. Lesson 3 uses exactly this JSON fluency to call real REST APIs from PowerShell.