internal/functions/Export-CsvData.ps1

function Export-CsvData {
    <#
    .SYNOPSIS
        Helper command that tees input objects into a CSV file.
     
    .DESCRIPTION
        Helper command that tees input objects into a CSV file.
     
    .PARAMETER Path
        The path to the file to be written to.
        May be empty, in which case no file will be created.
     
    .PARAMETER InputObject
        The object to write to CSV
     
    .EXAMPLE
        PS C:\> Get-ChildItem | Export-CsvData -Path C:\temp\files.csv | Where-Object Name -like "W*"
 
        Gets all files and folders in the current path, then writes all results to the specified csv file, then returns all items that start with a W.
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, Position = 0)]
        [AllowEmptyString()]
        [string]
        $Path,

        [Parameter(ValueFromPipeline = $true)]
        $InputObject
    )

    begin {
        if (-not $Path) { return }
        $command = { Export-Csv -Path $Path }.GetSteppablePipeline()
        $command.Begin($true)
    }
    process {
        if (-not $Path) { return $InputObject }
        $command.Process($InputObject)
        $InputObject
    }
    end {
        $command.End()
    }
}