Skip to content

Commit 61f8403

Browse files
authored
Fix pipeline example (#12229)
1 parent 8ec07fe commit 61f8403

File tree

4 files changed

+80
-36
lines changed

4 files changed

+80
-36
lines changed

reference/5.1/Microsoft.PowerShell.Core/About/about_Functions.md

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes how to create and use functions in PowerShell.
33
Locale: en-US
4-
ms.date: 04/17/2025
4+
ms.date: 07/16/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Functions
@@ -509,22 +509,33 @@ function Get-SumOfNumbers {
509509
begin { $retValue = 0 }
510510
511511
process {
512-
if ($null -ne $Numbers) {
512+
if ($MyInvocation.ExpectingInput) {
513+
"Pipeline input: $_"
514+
$retValue += $_
515+
} else {
513516
foreach ($n in $Numbers) {
517+
"Command line input: $n"
514518
$retValue += $n
515519
}
516-
} else {
517-
$retValue += $_
518520
}
519521
}
520522
521-
end { $retValue }
523+
end { "Sum = $retValue" }
522524
}
523525
524-
PS> 1, 2, 3, 4 | Get-SumOfNumbers
525-
10
526-
PS> Get-SumOfNumbers 1, 2, 3, 4
527-
10
526+
527+
PS> 1,2,3,4 | Get-SumOfNumbers
528+
Pipeline input: 1
529+
Pipeline input: 2
530+
Pipeline input: 3
531+
Pipeline input: 4
532+
Sum = 10
533+
PS> Get-SumOfNumbers 1,2,3,4
534+
Command line input: 1
535+
Command line input: 2
536+
Command line input: 3
537+
Command line input: 4
538+
Sum = 10
528539
```
529540

530541
When you use a function in a pipeline, the objects piped to the function are

reference/7.4/Microsoft.PowerShell.Core/About/about_Functions.md

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes how to create and use functions in PowerShell.
33
Locale: en-US
4-
ms.date: 04/17/2025
4+
ms.date: 07/16/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7.4&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Functions
@@ -533,22 +533,33 @@ function Get-SumOfNumbers {
533533
begin { $retValue = 0 }
534534
535535
process {
536-
if ($null -ne $Numbers) {
536+
if ($MyInvocation.ExpectingInput) {
537+
"Pipeline input: $_"
538+
$retValue += $_
539+
} else {
537540
foreach ($n in $Numbers) {
541+
"Command line input: $n"
538542
$retValue += $n
539543
}
540-
} else {
541-
$retValue += $_
542544
}
543545
}
544546
545-
end { $retValue }
547+
end { "Sum = $retValue" }
546548
}
547549
548-
PS> 1, 2, 3, 4 | Get-SumOfNumbers
549-
10
550-
PS> Get-SumOfNumbers 1, 2, 3, 4
551-
10
550+
551+
PS> 1,2,3,4 | Get-SumOfNumbers
552+
Pipeline input: 1
553+
Pipeline input: 2
554+
Pipeline input: 3
555+
Pipeline input: 4
556+
Sum = 10
557+
PS> Get-SumOfNumbers 1,2,3,4
558+
Command line input: 1
559+
Command line input: 2
560+
Command line input: 3
561+
Command line input: 4
562+
Sum = 10
552563
```
553564

554565
When you use a function in a pipeline, the objects piped to the function are

reference/7.5/Microsoft.PowerShell.Core/About/about_Functions.md

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes how to create and use functions in PowerShell.
33
Locale: en-US
4-
ms.date: 04/17/2025
4+
ms.date: 07/16/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7.5&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Functions
@@ -533,22 +533,33 @@ function Get-SumOfNumbers {
533533
begin { $retValue = 0 }
534534
535535
process {
536-
if ($null -ne $Numbers) {
536+
if ($MyInvocation.ExpectingInput) {
537+
"Pipeline input: $_"
538+
$retValue += $_
539+
} else {
537540
foreach ($n in $Numbers) {
541+
"Command line input: $n"
538542
$retValue += $n
539543
}
540-
} else {
541-
$retValue += $_
542544
}
543545
}
544546
545-
end { $retValue }
547+
end { "Sum = $retValue" }
546548
}
547549
548-
PS> 1, 2, 3, 4 | Get-SumOfNumbers
549-
10
550-
PS> Get-SumOfNumbers 1, 2, 3, 4
551-
10
550+
551+
PS> 1,2,3,4 | Get-SumOfNumbers
552+
Pipeline input: 1
553+
Pipeline input: 2
554+
Pipeline input: 3
555+
Pipeline input: 4
556+
Sum = 10
557+
PS> Get-SumOfNumbers 1,2,3,4
558+
Command line input: 1
559+
Command line input: 2
560+
Command line input: 3
561+
Command line input: 4
562+
Sum = 10
552563
```
553564

554565
When you use a function in a pipeline, the objects piped to the function are

reference/7.6/Microsoft.PowerShell.Core/About/about_Functions.md

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes how to create and use functions in PowerShell.
33
Locale: en-US
4-
ms.date: 04/17/2025
4+
ms.date: 07/16/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7.6&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Functions
@@ -533,22 +533,33 @@ function Get-SumOfNumbers {
533533
begin { $retValue = 0 }
534534
535535
process {
536-
if ($null -ne $Numbers) {
536+
if ($MyInvocation.ExpectingInput) {
537+
"Pipeline input: $_"
538+
$retValue += $_
539+
} else {
537540
foreach ($n in $Numbers) {
541+
"Command line input: $n"
538542
$retValue += $n
539543
}
540-
} else {
541-
$retValue += $_
542544
}
543545
}
544546
545-
end { $retValue }
547+
end { "Sum = $retValue" }
546548
}
547549
548-
PS> 1, 2, 3, 4 | Get-SumOfNumbers
549-
10
550-
PS> Get-SumOfNumbers 1, 2, 3, 4
551-
10
550+
551+
PS> 1,2,3,4 | Get-SumOfNumbers
552+
Pipeline input: 1
553+
Pipeline input: 2
554+
Pipeline input: 3
555+
Pipeline input: 4
556+
Sum = 10
557+
PS> Get-SumOfNumbers 1,2,3,4
558+
Command line input: 1
559+
Command line input: 2
560+
Command line input: 3
561+
Command line input: 4
562+
Sum = 10
552563
```
553564

554565
When you use a function in a pipeline, the objects piped to the function are

0 commit comments

Comments
 (0)