@@ -7,73 +7,83 @@ title: about_Return
7
7
---
8
8
# About Return
9
9
10
- ## SHORT DESCRIPTION
10
+ ## Short description
11
11
12
12
Exits the current scope, which can be a function, script, or script block.
13
13
14
- ## LONG DESCRIPTION
14
+ ## Long description
15
15
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.
19
19
20
20
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.
22
22
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.
26
26
27
- ### SYNTAX
27
+ ### Syntax
28
28
29
- The syntax for the Return keyword is as follows:
29
+ The syntax for the ` return ` keyword is as follows:
30
30
31
31
```
32
32
return [<expression>]
33
33
```
34
34
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
36
36
expression, as follows:
37
37
38
- ```
38
+ ``` powershell
39
39
return
40
40
return $a
41
41
return (2 + $a)
42
42
```
43
43
44
+ ### Examples
44
45
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.
46
49
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
52
52
{
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
55
57
}
56
58
57
- foreach ($a in @(get-wmiobject win32_desktop)) { ScreenPassword($a) }
59
+ 1..10 | ForEach-Object {MultiplyEven -Number $_ }
58
60
```
59
61
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
+ ```
64
74
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.
66
76
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:
68
78
69
- ```
79
+ ``` powershell
70
80
$a
71
81
return
72
82
```
73
83
74
- The following statement also returns the value of $a :
84
+ The following statement also returns the value of ` $a ` :
75
85
76
- ```
86
+ ``` powershell
77
87
return $a
78
88
```
79
89
@@ -88,33 +98,106 @@ function calculation {
88
98
$value += 73
89
99
return $value
90
100
}
91
- ```
92
-
93
- Running this function and assigning the result to a variable has the following
94
- effect:
95
101
96
- ``` powershell
97
102
$a = calculation 14
98
103
```
99
104
100
105
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:
102
107
103
- ``` output
108
+ ```
104
109
PS> $a
105
110
Please wait. Working on calculation...
106
111
87
107
112
```
108
113
109
114
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
+ ```
111
145
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
113
196
114
197
[ about_Language_Keywords] ( about_Language_Keywords.md )
115
198
116
199
[ about_Functions] ( about_Functions.md )
117
200
118
201
[ about_Scopes] ( about_Scopes.md )
119
202
120
- [ about_Script_Blocks] ( about_Script_Blocks.md )
203
+ [ about_Script_Blocks] ( about_Script_Blocks.md )
0 commit comments