shell-mastery — PowerShell jq/yq

jq and yq on PowerShell

Install (Windows)
winget install jqlang.jq
winget install MikeFarah.yq
# Or: choco install jq yq
Verify
jq --version
yq --version

jq on PowerShell

Pipe PowerShell output to jq
Get-Process | ConvertTo-Json | jq '.[0] | {name: .ProcessName, pid: .Id, mem: .WorkingSet64}'
curl + jq (same as bash)
curl -s "https://api.github.com/users/EvanusModestus" | jq '{login, public_repos, created_at}'
PowerShell objects → JSON → jq pipeline
Get-NetAdapter | ConvertTo-Json | jq '[.[] | {name: .Name, status: .Status, speed: .LinkSpeed}]'
Get-Service → jq filtering
Get-Service | ConvertTo-Json | jq '[.[] | select(.Status == "Running") | .Name] | length'
Network interfaces → jq
Get-NetIPAddress | ConvertTo-Json | jq '[.[] | select(.AddressFamily -eq 2) | {iface: .InterfaceAlias, ip: .IPAddress, prefix: .PrefixLength}]'
Event log → jq analysis
Get-WinEvent -LogName Security -MaxEvents 100 | ConvertTo-Json | jq '[.[] | {id: .Id, time: .TimeCreated, msg: .Message[:80]}]'
jq with Invoke-RestMethod (PowerShell native HTTP)
$data = Invoke-RestMethod "https://jsonplaceholder.typicode.com/users"
$data | ConvertTo-Json -Depth 5 | jq '[.[] | {name, email, city: .address.city}]'
ConvertTo-Json vs jq — when to use which
# PowerShell native (objects stay as objects)
Get-Process | Where-Object { $_.CPU -gt 100 } | Select-Object Name, CPU, Id | Format-Table

# jq (when you need text output, CSV, or complex transforms)
Get-Process | ConvertTo-Json | jq -r '.[] | select(.CPU > 100) | [.ProcessName, .CPU, .Id] | @csv'

yq on PowerShell

Read YAML
yq '.name' antora.yml
yq '.asciidoc.attributes | keys' antora.yml
Filter YAML attributes
yq '.asciidoc.attributes | to_entries | map(select(.key | test("^port-")))' antora.yml
Convert YAML to JSON
yq -o json antora.yml | jq '.'
Edit YAML in-place
yq -i '.version = "2.0.0"' config.yml
Merge YAML files
yq eval-all '. as $item ireduce ({}; . * $item)' base.yml override.yml
Parse docker-compose
yq '.services | keys' docker-compose.yml
yq '.services[] | .image' docker-compose.yml
PowerShell YAML parsing (native, no yq)
# Install powershell-yaml module
Install-Module -Name powershell-yaml -Scope CurrentUser
Import-Module powershell-yaml
$config = Get-Content antora.yml -Raw | ConvertFrom-Yaml
$config.asciidoc.attributes