PowerShell Genie

Write a correlation-aware JSON operation log

Script 1 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Each line is a complete JSON object with an ISO-8601 UTC timestamp and one correlation ID.

The script

1PS> $correlationId = [guid]::NewGuid().Guid
2PS> $log = '.\operation.jsonl'
3PS> function Add-OperationLog {
4PS> [CmdletBinding()]
5PS> param([string]$Level, [string]$Message, [hashtable]$Data = @{})
6PS> [pscustomobject]@{ Timestamp = (Get-Date).ToUniversalTime().ToString('o');CorrelationId = $correlationId;Level = $Level;Message = $Message;Data = $Data } |
7PS> ConvertTo-Json -Compress -Depth 6 | Add-Content -LiteralPath $log -Encoding utf8 }
8PS> Add-OperationLog -Level Information -Message 'Inventory started' -Data @{ Computer = $env:COMPUTERNAME }

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1 {"Timestamp":"2026-09-10T12:00:00Z","CorrelationId":"11111111-2222-3333-4444-555555555555","Level":"Information","Message":"Operation completed","Data":{"Operation":"Inventory"}}
    Inspect this example’s structure

    Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

    $correlationId = [guid]::NewGuid().Guid $log = '.\operation.jsonl' function Add-OperationLog { [CmdletBinding()] param([string]$Level, [string]$Message, [hashtable]$Data = @{}) [pscustomobject]@{ Timestamp = (Get-Date).ToUniversalTime().ToString('o');CorrelationId = $correlationId;Level = $Level;Message = $Message;Data = $Data } | ConvertTo-Json -Compress -Depth 6 | Add-Content -LiteralPath $log -Encoding utf8 } Add-OperationLog -Level Information -Message 'Inventory started' -Data @{ Computer = $env:COMPUTERNAME }
    1. Stage 1$correlationIdPositional values or no named parameters

      How it works

      Each line is a complete JSON object with an ISO-8601 UTC timestamp and one correlation ID. JSON Lines keeps appends cheap and lets a collector process records one at a time. The Data field preserves task-specific context without changing the top-level schema.

      Adapting it

      Add operation name, script version and duration when those are useful across runs. Send the same object to your logging API instead of Add-Content when a central collector exists.

      Primary documentation

      ConvertTo-Json reference

      Continue
      Genie guideWrite a correlation-aware JSON operation log

      Each line is a complete JSON object with an ISO-8601 UTC timestamp and one correlation ID.