# Pipeline call function

**URL:** <https://forums.powershell.org/t/pipeline-call-function/4244>\
**Category:** PowerShell Help\
**Created:** [May 20, 2015, 10:30pm UTC](https://forums.powershell.org/t/pipeline-call-function/4244 "2015-05-20T22:30:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adnan-salahuddin](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@adnan-salahuddin](https://forums.powershell.org/u/adnan-salahuddin)\
**Post date:** [May 20, 2015, 10:30pm UTC](https://forums.powershell.org/t/pipeline-call-function/4244/1 "2015-05-20T22:30:01Z")

</div>

Hi developer,

How to pipeline call a method or function if possible? e.g

Function.ps1  
function CommaSeparated  
{  
ForEach-Object {($\_ -replace “\s+”,“,”)}  
}

aa.ps1  
Import-Module “c:.…Function.ps1”  
Get-Content .\test1.txt | CommaSeparated | …

bb.ps1  
Import-Module “c:.…Function.ps1”  
Get-Content .\test2.txt | CommaSeparated | …

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [May 20, 2015, 10:37pm UTC](https://forums.powershell.org/t/pipeline-call-function/4244/2 "2015-05-20T22:37:10Z")

</div>

You’d need to set up your function to accept pipeline input, and use a process block. However, in this case, it may be even faster to just use a filter (which is like a function that only has a process block, and also happens to execute quite a bit faster):

```
filter CommaSeparated
{
    $_ -replace "\s+",","
}
```

---

<div class="post-metadata">

**Author:** ![adnan-salahuddin](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@adnan-salahuddin](https://forums.powershell.org/u/adnan-salahuddin)\
**Post date:** [May 20, 2015, 10:57pm UTC](https://forums.powershell.org/t/pipeline-call-function/4244/3 "2015-05-20T22:57:37Z")

</div>

thank you for replying back to me

I need tutorial about only function to input it and process block  
Do you have any link or reference?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [May 20, 2015, 11:07pm UTC](https://forums.powershell.org/t/pipeline-call-function/4244/4 "2015-05-20T23:07:31Z")

</div>

That information is mainly in the about\_Functions and about\_Functions\_Advanced\_Parameters help files. In its simplest form, that would look like this:

```
function CommaSeparated
{
    process
    {
        $_ -replace "\s+",","
    }
}
```

A more advanced approach (allowing for different ways of calling the function) would be something like this:

```
function CommaSeparated
{
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $true)]
        [string[]] $String
    )

    process
    {
        foreach ($s in $String)
        {
            $s -replace "\s+",","
        }
    }
}
```

This approach of accepting both pipeline input, as well as an array passed via the command line (eg, CommaSeparated -String ‘one two’, ‘three four’, ‘five six’) is a common user-friendly experience for functions that you plan to share with others.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:45pm UTC](https://forums.powershell.org/t/pipeline-call-function/4244/5 "2024-05-16T20:45:16Z")

</div>

Closed on May 16, 2024, 8:45pm UTC


