Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down