diff --git a/reference/5.1/Microsoft.PowerShell.Core/About/about_Functions.md b/reference/5.1/Microsoft.PowerShell.Core/About/about_Functions.md index 6a14dfe88f7..92d90c00e0c 100644 --- a/reference/5.1/Microsoft.PowerShell.Core/About/about_Functions.md +++ b/reference/5.1/Microsoft.PowerShell.Core/About/about_Functions.md @@ -1,7 +1,7 @@ --- description: Describes how to create and use functions in PowerShell. Locale: en-US -ms.date: 04/17/2025 +ms.date: 07/16/2025 online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-5.1&WT.mc_id=ps-gethelp schema: 2.0.0 title: about_Functions @@ -509,22 +509,33 @@ function Get-SumOfNumbers { begin { $retValue = 0 } process { - if ($null -ne $Numbers) { + if ($MyInvocation.ExpectingInput) { + "Pipeline input: $_" + $retValue += $_ + } else { foreach ($n in $Numbers) { + "Command line input: $n" $retValue += $n } - } else { - $retValue += $_ } } - end { $retValue } + end { "Sum = $retValue" } } -PS> 1, 2, 3, 4 | Get-SumOfNumbers -10 -PS> Get-SumOfNumbers 1, 2, 3, 4 -10 + +PS> 1,2,3,4 | Get-SumOfNumbers +Pipeline input: 1 +Pipeline input: 2 +Pipeline input: 3 +Pipeline input: 4 +Sum = 10 +PS> Get-SumOfNumbers 1,2,3,4 +Command line input: 1 +Command line input: 2 +Command line input: 3 +Command line input: 4 +Sum = 10 ``` When you use a function in a pipeline, the objects piped to the function are diff --git a/reference/7.4/Microsoft.PowerShell.Core/About/about_Functions.md b/reference/7.4/Microsoft.PowerShell.Core/About/about_Functions.md index 6525ccda9f5..c952b411cca 100644 --- a/reference/7.4/Microsoft.PowerShell.Core/About/about_Functions.md +++ b/reference/7.4/Microsoft.PowerShell.Core/About/about_Functions.md @@ -1,7 +1,7 @@ --- description: Describes how to create and use functions in PowerShell. Locale: en-US -ms.date: 04/17/2025 +ms.date: 07/16/2025 online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7.4&WT.mc_id=ps-gethelp schema: 2.0.0 title: about_Functions @@ -533,22 +533,33 @@ function Get-SumOfNumbers { begin { $retValue = 0 } process { - if ($null -ne $Numbers) { + if ($MyInvocation.ExpectingInput) { + "Pipeline input: $_" + $retValue += $_ + } else { foreach ($n in $Numbers) { + "Command line input: $n" $retValue += $n } - } else { - $retValue += $_ } } - end { $retValue } + end { "Sum = $retValue" } } -PS> 1, 2, 3, 4 | Get-SumOfNumbers -10 -PS> Get-SumOfNumbers 1, 2, 3, 4 -10 + +PS> 1,2,3,4 | Get-SumOfNumbers +Pipeline input: 1 +Pipeline input: 2 +Pipeline input: 3 +Pipeline input: 4 +Sum = 10 +PS> Get-SumOfNumbers 1,2,3,4 +Command line input: 1 +Command line input: 2 +Command line input: 3 +Command line input: 4 +Sum = 10 ``` When you use a function in a pipeline, the objects piped to the function are diff --git a/reference/7.5/Microsoft.PowerShell.Core/About/about_Functions.md b/reference/7.5/Microsoft.PowerShell.Core/About/about_Functions.md index fe15587f72d..1fdad8bafa4 100644 --- a/reference/7.5/Microsoft.PowerShell.Core/About/about_Functions.md +++ b/reference/7.5/Microsoft.PowerShell.Core/About/about_Functions.md @@ -1,7 +1,7 @@ --- description: Describes how to create and use functions in PowerShell. Locale: en-US -ms.date: 04/17/2025 +ms.date: 07/16/2025 online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7.5&WT.mc_id=ps-gethelp schema: 2.0.0 title: about_Functions @@ -533,22 +533,33 @@ function Get-SumOfNumbers { begin { $retValue = 0 } process { - if ($null -ne $Numbers) { + if ($MyInvocation.ExpectingInput) { + "Pipeline input: $_" + $retValue += $_ + } else { foreach ($n in $Numbers) { + "Command line input: $n" $retValue += $n } - } else { - $retValue += $_ } } - end { $retValue } + end { "Sum = $retValue" } } -PS> 1, 2, 3, 4 | Get-SumOfNumbers -10 -PS> Get-SumOfNumbers 1, 2, 3, 4 -10 + +PS> 1,2,3,4 | Get-SumOfNumbers +Pipeline input: 1 +Pipeline input: 2 +Pipeline input: 3 +Pipeline input: 4 +Sum = 10 +PS> Get-SumOfNumbers 1,2,3,4 +Command line input: 1 +Command line input: 2 +Command line input: 3 +Command line input: 4 +Sum = 10 ``` When you use a function in a pipeline, the objects piped to the function are diff --git a/reference/7.6/Microsoft.PowerShell.Core/About/about_Functions.md b/reference/7.6/Microsoft.PowerShell.Core/About/about_Functions.md index 26d85605329..81e902901c9 100644 --- a/reference/7.6/Microsoft.PowerShell.Core/About/about_Functions.md +++ b/reference/7.6/Microsoft.PowerShell.Core/About/about_Functions.md @@ -1,7 +1,7 @@ --- description: Describes how to create and use functions in PowerShell. Locale: en-US -ms.date: 04/17/2025 +ms.date: 07/16/2025 online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7.6&WT.mc_id=ps-gethelp schema: 2.0.0 title: about_Functions @@ -533,22 +533,33 @@ function Get-SumOfNumbers { begin { $retValue = 0 } process { - if ($null -ne $Numbers) { + if ($MyInvocation.ExpectingInput) { + "Pipeline input: $_" + $retValue += $_ + } else { foreach ($n in $Numbers) { + "Command line input: $n" $retValue += $n } - } else { - $retValue += $_ } } - end { $retValue } + end { "Sum = $retValue" } } -PS> 1, 2, 3, 4 | Get-SumOfNumbers -10 -PS> Get-SumOfNumbers 1, 2, 3, 4 -10 + +PS> 1,2,3,4 | Get-SumOfNumbers +Pipeline input: 1 +Pipeline input: 2 +Pipeline input: 3 +Pipeline input: 4 +Sum = 10 +PS> Get-SumOfNumbers 1,2,3,4 +Command line input: 1 +Command line input: 2 +Command line input: 3 +Command line input: 4 +Sum = 10 ``` When you use a function in a pipeline, the objects piped to the function are