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):
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.