You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/document/PowerShell/docs/Language/Function.md
+33-27Lines changed: 33 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,5 @@
1
1
# Function
2
2
3
-
4
-
## Parameter
5
-
6
3
Parameters are wrapped inside a function block with `param(...)`
7
4
8
5
```ps1
@@ -17,7 +14,7 @@ function Foo {
17
14
>
18
15
> Default type of a parameter is `System.Object`.
19
16
20
-
###Implicit Parameter
17
+
## Implicit Parameter
21
18
22
19
If no parameter name was set, all value passed in will be captured by `$args`, an `object[]`.
23
20
@@ -31,7 +28,10 @@ function vim {
31
28
vim ./foo.txt
32
29
```
33
30
34
-
### Positional Parameter
31
+
> [!NOTE]
32
+
> `$args` is not available when any of `ParameterAttribute` and `CmdletBinding` is applied on the function.
33
+
34
+
## Positional Parameter
35
35
36
36
Positional parameters allows passing values with explicit names.
37
37
@@ -54,9 +54,9 @@ Or use a explicit position argument on attribute.
54
54
```ps1
55
55
function Foo {
56
56
param (
57
-
[Parameter(Position=1)] # [!code highlight]
57
+
[Parameter(Position = 1)] # [!code highlight]
58
58
[string] $Bar
59
-
[Parameter(Position=0)] # [!code highlight]
59
+
[Parameter(Position = 0)] # [!code highlight]
60
60
[string] $Foo,
61
61
)
62
62
@@ -67,7 +67,14 @@ Foo -Foo foo -Bar bar
67
67
Foo foo bar # it's the same # [!code highlight]
68
68
```
69
69
70
-
### Flags
70
+
PowerShell starts counting the position when there's a value belonging to no explicit parameter name.
71
+
Assuming `-Flag` is a switch, `-Foo` has position `0`, the value `foo` will be assigned to `-Foo`.
72
+
73
+
```ps1
74
+
Foo -Flag foo
75
+
```
76
+
77
+
## Flags
71
78
72
79
Defining flags that represents a toggle needs a special type called `switch`.
73
80
`switch` has the same nature of `bool`, but `bool` parameter requires explicit assignment when the function being called.
@@ -91,7 +98,7 @@ Manual assignment is also available:
91
98
Foo -f:$false -b $true
92
99
```
93
100
94
-
###Default Parameter
101
+
## Default Parameter
95
102
96
103
- Explicitly typed parameters can have implicit default value.
97
104
-`switch` and `bool` is `$false` by default.
@@ -117,7 +124,7 @@ function Foo {
117
124
> [!NOTE]
118
125
> For overriding default parameter outside the function, see [$PSDefaultParameterValues](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parameters_default_values?view=powershell-7.4#long-description)
119
126
120
-
###Required Parameter
127
+
## Required Parameter
121
128
122
129
All parameters are optional by default. Use `[Parameter(Mandatory=$true)]` to mark it as required.
123
130
@@ -137,7 +144,7 @@ param (
137
144
>)
138
145
>```
139
146
140
-
### Parameter Alias
147
+
## Parameter Alias
141
148
142
149
Parameters can have aliases. It's not needed for most of time though since pwsh can distinguish option by leading string.
143
150
@@ -156,7 +163,7 @@ function Person {
156
163
Person -n "Alice" -a 30 # [!code highlight]
157
164
```
158
165
159
-
###Parameter Validation
166
+
## Parameter Validation
160
167
161
168
Pass a validation logic as script block to `ValidateScript` attribute, `$_` represents singular value of the parameter or current item of a collection.
162
169
Will throw an error if any parameter does not satisfies the condition.
@@ -197,7 +204,7 @@ param (
197
204
)
198
205
```
199
206
200
-
### Pass By Reference
207
+
## Pass By Reference
201
208
202
209
Parameter passed by reference is implemented by a wrapper `System.Management.Automation.PSReference`.
203
210
Value types are passed by value by default, the pass as reference, mark the parameter with `[ref]`.
@@ -278,7 +285,7 @@ A function would generally not acting a cmdlet unless it was annotated with `Cmd
278
285
279
286
`CmdletBinding()` can have the following properties:
280
287
281
-
-`DefaultParameterSetName`: name of implicit Parameter Set
288
+
-`DefaultParameterSetName`: pwsh will prefer this name when there's a ambiguity between syntax provided.
282
289
-`HelpURI`: link to documenetation
283
290
-`SupportsPaging`: implicitly adds parameters `-First`, `-Skip`, `-IncludeTotalCount`, value accessible by `$PSCmdlet.PagingParameters`
284
291
```ps1
@@ -302,7 +309,7 @@ How a same cmdlet manage different syntax for different usages? The trick is **P
302
309
Parameter Set is a classification on paramater to distinguish or limit the use of parameters from scenarios.
303
310
304
311
- a parameter set must have at least one unique parameter to others to identify the set
305
-
- a parameter can have multiple Parameter Set
312
+
- a parameter can be member of multiple parameter sets.
306
313
- a parameter can have different roles in different Parameter Set, can be mandatory in one and optional in another
307
314
- a parameter without explicit Parameter Set belongs to all other Parameter Set
308
315
- at least one parameter in the Parameter Set is mandatory
@@ -312,7 +319,6 @@ Parameter Set is a classification on paramater to distinguish or limit the use o
312
319
313
320
`$PSCmdlet.ParameterSetName` reflects the Parameter Set been chosen when a cmdlet is executing with certain syntax.
314
321
315
-
316
322
## Common Parameters
317
323
318
324
Any function or cmdlet applied with `CmdletBinding()` or `Parameter()` attribute has the following implicit parameters added by PowerShell:
@@ -334,7 +340,7 @@ Any function or cmdlet applied with `CmdletBinding()` or `Parameter()` attribute
334
340
- ProgressAction (proga)
335
341
- OutVariable (ov): declare **inline** and store the output to the variable. Similar to `ev`.
336
342
It's interesting that `-OutVariable` collects incremnentally.
337
-
It collects new item from pipeline on iteration.
343
+
It collects new item from pipeline on each iteration.
338
344
```ps1
339
345
1..5 | % { $_ } -OutVariable foo | % { "I am $foo" }
340
346
# I am 1
@@ -349,14 +355,16 @@ Any function or cmdlet applied with `CmdletBinding()` or `Parameter()` attribute
Intrinsic methods and properties are to mimic singular object and collection in a same form.
38
-
-`Where`: a method for filtering or slicing by condition. See [Where](../Object Manipulation/Where.md)
39
-
-`ForEach`: a method to perform iteration with certain logic or perform casting all items to target type. See [ForEach](../Object Manipulation/ForEach.md)
42
+
- `Where`: a method for filtering or slicing by condition. See [Where](/docs/document/PowerShell/docs/Object%20Manipulation/3.Where.md)
43
+
- `ForEach`: a method to perform iteration with certain logic or perform casting all items to target type. See [ForEach](/docs/document/PowerShell/docs/Object%20Manipulation/4.ForEach.md#intrinsic-foreach)
0 commit comments