Skip to content

Commit b9e104c

Browse files
committed
Fixes MicrosoftDocs#3290, Fixes MicrosoftDocs#3285 Rewrite topics to explain automatic collection enumeration
1 parent cd1058b commit b9e104c

File tree

20 files changed

+883
-223
lines changed

20 files changed

+883
-223
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ positional, switch, or dynamic parameters. Function parameters can be read
2929
from the command line or from the pipeline.
3030

3131
Functions can return values that can be displayed, assigned to variables, or
32-
passed to other functions or cmdlets.
32+
passed to other functions or cmdlets. You can also specify a return value using
33+
the `return` keyword. The `return` keyword does not affect or suppress other
34+
output returned from your function. You can also use the `return` keyword to
35+
exit your function. For more information, see
36+
[about_Return](about_Return.md).
3337

3438
The function's statement list can contain different types of statement lists
3539
with the keywords `Begin`, `Process`, and `End`. These statement lists

reference/3.0/Microsoft.PowerShell.Core/About/about_Return.md

Lines changed: 124 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,73 +7,83 @@ title: about_Return
77
---
88
# About Return
99

10-
## SHORT DESCRIPTION
10+
## Short description
1111

1212
Exits the current scope, which can be a function, script, or script block.
1313

14-
## LONG DESCRIPTION
14+
## Long description
1515

16-
The Return keyword exits a function, script, or script block. It can be used
17-
to exit a scope at a specific point, to return a value, or to indicate that
18-
the end of the scope has been reached.
16+
The `return` keyword exits a function, script, or script block. It can be used
17+
to exit a scope at a specific point, to return a value, or to indicate that the
18+
end of the scope has been reached.
1919

2020
Users who are familiar with languages like C or C\# might want to use the
21-
Return keyword to make the logic of leaving a scope explicit.
21+
`return` keyword to make the logic of leaving a scope explicit.
2222

23-
In PowerShell, the results of each statement are returned as output,
24-
even without a statement that contains the Return keyword. Languages like C or
25-
C\# return only the value or values that are specified by the Return keyword.
23+
In PowerShell, the results of each statement are returned as output, even
24+
without a statement that contains the Return keyword. Languages like C or C\#
25+
return only the value or values that are specified by the `return` keyword.
2626

27-
### SYNTAX
27+
### Syntax
2828

29-
The syntax for the Return keyword is as follows:
29+
The syntax for the `return` keyword is as follows:
3030

3131
```
3232
return [<expression>]
3333
```
3434

35-
The Return keyword can appear alone, or it can be followed by a value or
35+
The `return` keyword can appear alone, or it can be followed by a value or
3636
expression, as follows:
3737

38-
```
38+
```powershell
3939
return
4040
return $a
4141
return (2 + $a)
4242
```
4343

44+
### Examples
4445

45-
### EXAMPLES
46+
The following example uses the `return` keyword to exit a function at a
47+
specific point if a conditional is met. Odd numbers are not multiplied
48+
because the return statement exits before that statement can execute.
4649

47-
The following example uses the Return keyword to exit a function at a specific
48-
point if a conditional is met:
49-
50-
```
51-
function ScreenPassword($instance)
50+
```powershell
51+
function MultiplyEven
5252
{
53-
if (!($instance.screensaversecure)) {return $instance.name}
54-
<additional statements>
53+
param($number)
54+
55+
if ($number % 2) { return "$number is not even" }
56+
$number * 2
5557
}
5658
57-
foreach ($a in @(get-wmiobject win32_desktop)) { ScreenPassword($a) }
59+
1..10 | ForEach-Object {MultiplyEven -Number $_}
5860
```
5961

60-
This script checks each user account. The ScreenPassword function returns the
61-
name of any user account that does not have a password-protected screen saver.
62-
If the screen saver is password protected, the function completes any other
63-
statements to be run, and PowerShell does not return any value.
62+
```output
63+
1 is not even
64+
4
65+
3 is not even
66+
8
67+
5 is not even
68+
12
69+
7 is not even
70+
16
71+
9 is not even
72+
20
73+
```
6474

65-
In PowerShell, values can be returned even if the Return keyword is not used.
75+
In PowerShell, values can be returned even if the `return` keyword is not used.
6676
The results of each statement are returned. For example, the following
67-
statements return the value of the \$a variable:
77+
statements return the value of the `$a` variable:
6878

69-
```
79+
```powershell
7080
$a
7181
return
7282
```
7383

74-
The following statement also returns the value of $a:
84+
The following statement also returns the value of `$a`:
7585

76-
```
86+
```powershell
7787
return $a
7888
```
7989

@@ -88,33 +98,106 @@ function calculation {
8898
$value += 73
8999
return $value
90100
}
91-
```
92-
93-
Running this function and assigning the result to a variable has the following
94-
effect:
95101
96-
```powershell
97102
$a = calculation 14
98103
```
99104

100105
The "Please wait. Working on calculation..." string is not displayed. Instead,
101-
it is assigned to the $a variable, as in the following example:
106+
it is assigned to the `$a` variable, as in the following example:
102107

103-
```output
108+
```
104109
PS> $a
105110
Please wait. Working on calculation...
106111
87
107112
```
108113

109114
Both the informational string and the result of the calculation are returned
110-
by the function and assigned to the \$a variable.
115+
by the function and assigned to the `$a` variable.
116+
117+
### Return values and the Pipeline
118+
119+
When you return a collection from your script block or function, PowerShell
120+
automatically unrolls the members and passes them one at a time through the
121+
pipeline. This is due to PowerShell's one-at-a-time processing. For more
122+
information, see [about_pipelines](about_pipelines.md).
123+
124+
This concept is illustrated by the following sample function that returns an
125+
array of numbers. The output from the function is piped to the `Measure-Object`
126+
cmdlet which counts the number of objects in the pipeline.
127+
128+
```powershell
129+
function Test-Return
130+
{
131+
$array = 1,2,3
132+
return $array
133+
}
134+
Test-Return | Measure-Object
135+
```
136+
137+
```Output
138+
Count : 3
139+
Average :
140+
Sum :
141+
Maximum :
142+
Minimum :
143+
Property :
144+
```
111145

112-
## SEE ALSO
146+
To force a script block or function to return collection as a single
147+
object to the pipeline, use one of the following two methods:
148+
149+
- Unary array expression
150+
Utilizing a unary expression you can send your return value down the pipeline
151+
as a single object as illustrated by the following example.
152+
153+
```powershell
154+
function Test-Return
155+
{
156+
$array = 1,2,3
157+
return (, $array)
158+
}
159+
Test-Return | Measure-Object
160+
```
161+
162+
```Output
163+
Count : 1
164+
Average :
165+
Sum :
166+
Maximum :
167+
Minimum :
168+
Property :
169+
```
170+
171+
- `Write-Output` with **NoEnumerate** parameter.
172+
You can also use the `Write-Output` cmdlet with the **NoEnumerate** parameter.
173+
The example below uses the `Measure-Object` cmdlet to count the objects sent
174+
to the pipeline from the sample function by the `return` keyword.
175+
176+
```powershell
177+
function Test-Return
178+
{
179+
$array = 1, 2, 3
180+
return Write-Output -NoEnumerate $array
181+
}
182+
183+
Test-Return | Measure-Object
184+
```
185+
186+
```Output
187+
Count : 1
188+
Average :
189+
Sum :
190+
Maximum :
191+
Minimum :
192+
Property :
193+
```
194+
195+
## See also
113196

114197
[about_Language_Keywords](about_Language_Keywords.md)
115198

116199
[about_Functions](about_Functions.md)
117200

118201
[about_Scopes](about_Scopes.md)
119202

120-
[about_Script_Blocks](about_Script_Blocks.md)
203+
[about_Script_Blocks](about_Script_Blocks.md)

reference/3.0/Microsoft.PowerShell.Core/About/about_Script_Blocks.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ the following syntax:
2828
A script block returns the output of all the commands in the script block,
2929
either as a single object or as an array.
3030

31+
You can also specify a return value using the `return` keyword. The `return`
32+
keyword does not affect or suppress other output returned from your script
33+
block. You can also use the `return` keyword to exit your script block. For
34+
more information, see [about_Return](about_Return.md).
35+
3136
Like functions, a script block can include parameters. Use the Param
3237
keyword to assign named parameters, as shown in the following syntax:
3338

0 commit comments

Comments
 (0)