Skip to content

Commit a9dfa59

Browse files
committed
main
1 parent 0428e83 commit a9dfa59

File tree

5 files changed

+128
-45
lines changed

5 files changed

+128
-45
lines changed

docs/document/PowerShell/docs/Language/Array.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ $foo = @{
7575
@($foo.GetEnumerator()).Length # 2, System.Collections.DictionaryEntry from the HashTable # [!code highlight]
7676
```
7777

78+
### From Range
79+
80+
Range operator can create array from integer range and character range inclusively from both sides.
81+
82+
> [!NOTE]
83+
> character range was added in PowerShell 6
84+
85+
```ps1
86+
1..10 # 1 to 10 inclusively
87+
'a'..'z' # a to z inclusively
88+
```
89+
90+
If the *end* is less than the *start*, the array counts down from *start* to *end*
91+
92+
```ps1
93+
5..1 # 5 4 3 2 1
94+
5..-1 # 5 4 3 2 1 0 -1
95+
```
96+
7897
## Access Item
7998

8099
Powershell allows indexer syntax to access one or more items at a time or use `Select-Object`.

docs/document/PowerShell/docs/Language/Function.md

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# Function
22

3-
4-
## Parameter
5-
63
Parameters are wrapped inside a function block with `param(...)`
74

85
```ps1
@@ -17,7 +14,7 @@ function Foo {
1714
>
1815
> Default type of a parameter is `System.Object`.
1916
20-
### Implicit Parameter
17+
## Implicit Parameter
2118

2219
If no parameter name was set, all value passed in will be captured by `$args`, an `object[]`.
2320

@@ -31,7 +28,10 @@ function vim {
3128
vim ./foo.txt
3229
```
3330

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
3535

3636
Positional parameters allows passing values with explicit names.
3737

@@ -54,9 +54,9 @@ Or use a explicit position argument on attribute.
5454
```ps1
5555
function Foo {
5656
param (
57-
[Parameter(Position=1)] # [!code highlight]
57+
[Parameter(Position = 1)] # [!code highlight]
5858
[string] $Bar
59-
[Parameter(Position=0)] # [!code highlight]
59+
[Parameter(Position = 0)] # [!code highlight]
6060
[string] $Foo,
6161
)
6262
@@ -67,7 +67,14 @@ Foo -Foo foo -Bar bar
6767
Foo foo bar # it's the same # [!code highlight]
6868
```
6969

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
7178

7279
Defining flags that represents a toggle needs a special type called `switch`.
7380
`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:
9198
Foo -f:$false -b $true
9299
```
93100

94-
### Default Parameter
101+
## Default Parameter
95102

96103
- Explicitly typed parameters can have implicit default value.
97104
- `switch` and `bool` is `$false` by default.
@@ -117,7 +124,7 @@ function Foo {
117124
> [!NOTE]
118125
> 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)
119126
120-
### Required Parameter
127+
## Required Parameter
121128

122129
All parameters are optional by default. Use `[Parameter(Mandatory=$true)]` to mark it as required.
123130

@@ -137,7 +144,7 @@ param (
137144
>)
138145
>```
139146
140-
### Parameter Alias
147+
## Parameter Alias
141148
142149
Parameters can have aliases. It's not needed for most of time though since pwsh can distinguish option by leading string.
143150
@@ -156,7 +163,7 @@ function Person {
156163
Person -n "Alice" -a 30 # [!code highlight]
157164
```
158165
159-
### Parameter Validation
166+
## Parameter Validation
160167

161168
Pass a validation logic as script block to `ValidateScript` attribute, `$_` represents singular value of the parameter or current item of a collection.
162169
Will throw an error if any parameter does not satisfies the condition.
@@ -197,7 +204,7 @@ param (
197204
)
198205
```
199206
200-
### Pass By Reference
207+
## Pass By Reference
201208
202209
Parameter passed by reference is implemented by a wrapper `System.Management.Automation.PSReference`.
203210
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
278285

279286
`CmdletBinding()` can have the following properties:
280287

281-
- `DefaultParameterSetName`: name of implicit Parameter Set
288+
- `DefaultParameterSetName`: pwsh will prefer this name when there's a ambiguity between syntax provided.
282289
- `HelpURI`: link to documenetation
283290
- `SupportsPaging`: implicitly adds parameters `-First`, `-Skip`, `-IncludeTotalCount`, value accessible by `$PSCmdlet.PagingParameters`
284291
```ps1
@@ -302,7 +309,7 @@ How a same cmdlet manage different syntax for different usages? The trick is **P
302309
Parameter Set is a classification on paramater to distinguish or limit the use of parameters from scenarios.
303310
304311
- 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.
306313
- a parameter can have different roles in different Parameter Set, can be mandatory in one and optional in another
307314
- a parameter without explicit Parameter Set belongs to all other Parameter Set
308315
- 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
312319
313320
`$PSCmdlet.ParameterSetName` reflects the Parameter Set been chosen when a cmdlet is executing with certain syntax.
314321
315-
316322
## Common Parameters
317323
318324
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
334340
- ProgressAction (proga)
335341
- OutVariable (ov): declare **inline** and store the output to the variable. Similar to `ev`.
336342
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.
338344
```ps1
339345
1..5 | % { $_ } -OutVariable foo | % { "I am $foo" }
340346
# I am 1
@@ -349,14 +355,16 @@ Any function or cmdlet applied with `CmdletBinding()` or `Parameter()` attribute
349355
- PipelineVariable (pv) <!-- TODO: PipelineVariable -->
350356
- Verbose (vb): whether display the verbose message from `Write-Verbose`
351357
352-
## Mitigation Parameters
358+
### Mitigation Parameters
359+
360+
Mitigation parameters were added when `CmdletBinding(SupportsShouldProcess)` was applied.
353361
354-
- WhatIf (wi)
355-
- Confirm (cf)
362+
- WhatIf (wi): shows explaination for the command without executing it.
363+
- Confirm (cf): ask for confirmation when executing the command.
356364
357365
## Return
358366
359-
Powershell allows implicit return, and multiple implicit returns.
367+
PowerShell allows implicit return, and multiple implicit returns.
360368
361369
> [!NOTE]
362370
> Implicit returns are auto-collected as an array or single value.
@@ -400,11 +408,9 @@ PowerShell never do type checking by this attribute, it's just responsible for h
400408
[OutputType('System.Int32', ParameterSetName = 'ID')]
401409
[OutputType([string], ParameterSetName = 'Name')]
402410
param (
403-
[Parameter(Mandatory = $true, ParameterSetName = 'ID')]
404-
[int[]]
405-
$UserID,
406-
[Parameter(Mandatory = $true, ParameterSetName = 'Name')]
407-
[string[]]
408-
$UserName
411+
[Parameter(Mandatory, ParameterSetName = 'ID')]
412+
[int[]]$UserID,
413+
[Parameter(Mandatory, ParameterSetName = 'Name')]
414+
[string[]]$UserName
409415
)
410416
```

docs/document/PowerShell/docs/Language/String.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,29 @@ All comparison operators have its case-sensitive version with leading `-c`
136136
(gci -file) -join ',' # ToString is invoked to evaluated objects to string.
137137
```
138138

139+
### Join from Objects
140+
141+
`Join-String` is a more generic solution for joining multiple items as a single string.
142+
`-join` operator handles only collection while `Join-String` can accept pipeline input **by value**.
143+
144+
- `-Property(0)`: pick one property name or script block to be joined in the result
145+
```ps1
146+
gci -file | Join-String FullName
147+
gci -file | Join-String { $_.FullName }
148+
```
149+
- `-Separator(1)`: separator between each two properties
150+
```ps1
151+
gci -file | Join-String FullName ', '
152+
```
153+
- `-DoubleQuote`: optionally surround each item by double quote
154+
- `-SingleQuote`: optionally surround each item by single quote
155+
- `-OutputPrefix`: specify a initial leading string for the result
156+
- `-OutputSuffix`: specify a trailing string for the result
157+
- `-FormatString`: use string format to reshape the string from `-Property`(you can do the similar in `-Property` too)
158+
```ps1
159+
gci -file | Join-String Name "$([Environment]::NewLine)" -FormatString 'File name: {0}'
160+
```
161+
139162
## Match & Replace
140163
141164
PowerShell has two kinds of matching strategy for strings.
@@ -268,15 +291,17 @@ gci | Out-String | sls 'foo' # match the entire string
268291
gci | oss | sls 'foo' # match the matched line only
269292
```
270293

271-
## String Evaluation in Interpolation
294+
## String Evaluation
272295

273296
### Collection
274297

275-
Collections like Array and HashTable are formatted by certain separator defined by `$OFS`(Output Field Separator)
298+
Collections like Array and HashTable are formatted by separator stored in `$OFS`(Output Field Separator)
276299

277300
```ps1
278301
"$(1, 2, 3)" # 1 2 3
302+
$OFS = ', '
303+
"$(1, 2, 3)" # 1, 2, 3
279304
```
280305

281306
> [!NOTE]
282-
> `$OFS` is not a builtin variable, you'll have to create it manually or pwsh uses space ` ` as the separator.
307+
> `$OFS` is not a builtin variable, you'll have to create it manually or pwsh uses space ` ` as the default separator.

docs/document/PowerShell/docs/Language/Variable.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,35 @@ $var = 'foo'
1111
echo $var
1212
```
1313

14-
## Scope
14+
## Variable Provider
1515

16+
PowerShell has builtin PSProvider for all variables named `Variable:`
17+
18+
## Item Cmdlet for Variables
19+
20+
You may use Item cmdlets for manipulating variables since they're from a PSDrive.
21+
Typically useful when manipulating in batch.
22+
23+
When `-ItemType` unspecified and `-Path` were a path from variable drive, `New-Item` creates a variable.
24+
25+
```ps1
26+
ni -Path Variable:process -Name process -Value (gps)
27+
```
28+
29+
There's not much necessity for using Item cmdlets for variables, you may use them but it doesn't seem to be straightforward.
30+
31+
## Variable Cmdlet
32+
33+
- `Get-Variable`: retrieve variables from scope
34+
- `New-Variable`: create variables with more options available
35+
- `Set-Variable`: override value for variables
36+
- `Remove-Variable`: delete variables from scope or pattern
37+
- `Clear-Variable`: set variable to null
38+
39+
> [!NOTE]
40+
> All these variable cmdlets supports `-Name` that accepts pipeline input by property.
41+
42+
It's more recommended to use variable cmdlets since it supports more parameters to work with
43+
44+
- `Scope`: work with variables in certain scope
1645

docs/document/PowerShell/docs/Type System/Extended Type System.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,30 @@ There's two kinds of intrinsic members in PowerShell
1717
All objects in PowerShell have five **object views** members:
1818

1919
- `psobject`: a `MemberSet` containing reflection source of members.
20-
```ps1
21-
$foo = 'I am foo'
22-
[object]::ReferenceEquals($foo, $foo.psobject.BaseObject) # True
23-
```
20+
21+
```ps1
22+
$foo = 'I am foo'
23+
[object]::ReferenceEquals($foo, $foo.psobject.BaseObject) # True
24+
```
2425
- `psbase`: a `MemberSet` containing members of the object being wrapped.
25-
```ps1
26-
$foo = 'I am foo'
27-
[object]::ReferenceEquals($foo.ToString, $foo.psbase.ToString) # True
28-
```
26+
27+
```ps1
28+
$foo = 'I am foo'
29+
[object]::ReferenceEquals($foo.ToString, $foo.psbase.ToString) # True
30+
```
31+
2932
- `psadapted`: a `MemberSet` containing adapted members added by ETS.
3033
- `psextended`: a `MemberSet` containing extended members **added at runtime**.
3134
- `pstypenames`: a `CodeProperty` equivalent to `psobject.TypeNames`. Returns a collection containing the type names of inheritance chain.
32-
```ps1
33-
$foo = 'I am foo'
34-
[object]::ReferenceEquals($foo.psobject.TypeNames, $foo.pstypenames) # True
35-
```
35+
36+
```ps1
37+
$foo = 'I am foo'
38+
[object]::ReferenceEquals($foo.psobject.TypeNames, $foo.pstypenames) # True
39+
```
3640
3741
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)
4044
- `Count`
4145
- `Length`
4246

0 commit comments

Comments
 (0)