ninMonkey
ninMonkey

Reputation: 7501

How to support piping in my function

I want to be able to use my function like:

Get-Process | export-xsl;

Right now I'm manually calling Get-Process inside my function:

function export-xsl() {
    $path = "{0}\Downloads\test.csv" -f $home;
    Get-Process | export-csv -Path $path -NoTypeInformation
    Invoke-item $path;
}

The examples I found seem to iterate on each item, which I believe will create multiple .csv files.

I tried, but this creates the CSV dozens of times, once per iteration. I'm trying to get the entire object as one CSV file.

function export-xsl() {
    process {
        $path = "{0}\Downloads\test.csv" -f $home;
        $_ | export-csv -Path $path -NoTypeInformation
        Invoke-item $path;
    }
}

Upvotes: 0

Views: 59

Answers (1)

ninMonkey
ninMonkey

Reputation: 7501

function export-xsl {
    $path = "{0}\Downloads\test.csv" -f $home;
    $input | export-csv -Path $path -NoTypeInformation;
    Invoke-item $path;
}

$input will allow you to pipe all the data at once, instead of many iterations.

Upvotes: 1

Related Questions