PowerShell: Define Function to Accept Pipe

By Xah Lee. Date: .

Make Command Accept Pipe

# example of a command taking piped input

function ff {
    # make the function accept piped input
    process { $_ }
}

# HHHH------------------------------

# pipe to the function
dir *txt | ff
# output is array of files ending in txt

# call the function by itself
ff
# does nothing

Make Parameter accept pipe

xtodo work in progress
# example of a command
# when called without arg, it does (dir *txt)
# can accept pipe input

function my-list-file {
    # make a parameter to take piped input
    param ( [parameter(ValueFromPipeline = $true)] $filelist = (dir *txt))
    process { $filelist }
}

# call the function
my-list-file
# output is array of files ending in txt

# pipe to the function
dir *el | my-list-file
# output is array of files ending in el

PowerShell. Define Function