Private/Export-ShellPhishData.ps1

function Export-ShellPhishData {
    <#
    .SYNOPSIS
        Shared helper that writes data to CSV or JSON based on file extension, or outputs to console.
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [object]$Data,

        [Parameter(Mandatory=$false)]
        [string]$ExportPath,

        [Parameter(Mandatory=$false)]
        [string]$ConsoleProperty
    )

    if ($ExportPath) {
        $dir = Split-Path $ExportPath -Parent
        if ($dir -and -not (Test-Path $dir)) {
            New-Item -ItemType Directory -Path $dir -Force | Out-Null
        }

        switch -Regex ($ExportPath) {
            '\.csv$' {
                $Data | Export-Csv -Path $ExportPath -NoTypeInformation -Force
                Write-Host "Exported $($Data.Count) record(s) to $ExportPath"
            }
            '\.json$' {
                $Data | ConvertTo-Json -Depth 10 | Set-Content -Path $ExportPath -Force
                Write-Host "Exported $($Data.Count) record(s) to $ExportPath"
            }
            default {
                Write-Error "Unsupported file extension. Use .csv or .json"
            }
        }
    }
    else {
        $Data
    }
}