Skip to content

Commit 5a13b4d

Browse files
committed
main
1 parent d7651d1 commit 5a13b4d

File tree

6 files changed

+94
-13
lines changed

6 files changed

+94
-13
lines changed

docs/document/Powershell/docs/Language/Array.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,28 @@ To substract a collection from another collection, you can certainly use `LINQ`
154154
@(1,2,3) | where { @(1, 2) -notcontains $_ } # 3
155155
```
156156

157+
## Equality Checking
158+
159+
Collections behave differently in Powershell on equality checking, they're not based on reference but the contained items.
160+
161+
- `<arr> -eq <item>`: returns all items in `<arr>` equal to `<item>`.
162+
- `<arr> -ne <item>`: returns all items in `<arr>` not equal to `<item>`.
163+
- always returns `object[]` even for single result or no result.
164+
165+
```ps1
166+
1,1,2,3 -eq 1 # 1,1
167+
'a','b','c' -ne 'a' # 'b', 'c'
168+
169+
# the whole item is an array so no result is returned.
170+
1,1,2,3 -eq 1,2 # empty array
171+
```
172+
173+
> [!TIP]
174+
> You can use `-ne` to exclude single item from a collection.
175+
176+
> [!NOTE]
177+
> To do reference checking, use `[object]::ReferenceEquals`.
178+
157179
## Null Checking
158180

159181
Checking null for collections is a quirk in PowerShell, `$arr -eq $null` checks all items instead of the whole array.
@@ -164,10 +186,11 @@ $arr = 1,2,3
164186
$arr -eq $null # empty array
165187
166188
$null -eq $arr # False, the result we expected # [!code highlight]
167-
168-
if ($arr) { 'array is not null and not empty' } # check both empty and null
169189
```
170190

191+
> [!TIP]
192+
> Always leave array as the right operand on null checking.
193+
171194
## To List
172195

173196
PowerShell allows direct casting a array to an `ArrayList` or generic `List<T>`.
@@ -184,18 +207,20 @@ using namespace System.Collections.Generic
184207

185208
Keyword operators has special functionalities on collections.
186209
`-match`, `-notmatch`, `-replace`, `-split` handles for all items in the left operand collection, the result is always an array.
187-
If the item is not a string, Powershell evaluates it to string by certain strategy.
188210

189211
```ps1
190212
# Returns items that matches the regex
191213
@('John', 'Jane', 'Janet') -match 'Jane' # Jane, Janet.
192-
(gci -file) -match '.*txt$' # FileInfo[], files with FullName matches to the pattern
214+
(gci -file) -match '.*txt$' # FileInfo with FullName matches to the pattern
193215
(@('John', 'Jane', 'Janet') -notmatch 'Jane') -is [Array] # True, only John matches and still an array.
194216
195217
@('John', 'Jane', 'Janet') -replace 'J','K' # Kohn Kane Kanet
196218
'1,2,3','1,2,3' -split ',' # 1 2 3 1 2 3, strings
197219
```
198220

221+
> [!NOTE]
222+
> If current item for `-match` or `-notmatch` is not a string, Powershell evaluates it to string by certain strategy.
223+
199224
## Multi-Dim Array
200225

201226
You'll have to create Multi-Dim array from .NET type constructor only.

docs/document/Powershell/docs/Language/Function.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,27 @@ param (
204204
)
205205
```
206206

207+
### Pass By Reference
208+
209+
Parameter passed by reference is implemented by a wrapper `System.Management.Automation.PSReference`.
210+
Value types are passed by value by default, the pass as reference, mark the parameter with `[ref]`.
211+
Casting to `[ref]` generates a new wrapper containing the value.
212+
213+
```ps1
214+
function Foo {
215+
param ([ref][int]$foo) # [!code highlight]
216+
$foo.Value = 250
217+
$foo.Value
218+
}
219+
220+
$bar = 1
221+
Foo ([ref]$bar)
222+
$bar # 250 # [!code highlight]
223+
```
224+
225+
> [!NOTE]
226+
> `[ref]` can only be marked before type annotation.
227+
207228
## Named Blocks
208229

209230
In a simple function where there's only one series of parameters being taken, we don't have to use any complex logic.

docs/document/Powershell/docs/Language/HashTable.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# HashTable
22

3-
HashTable is a dynamicly typed data structure in PowerShell, it implements `IDictionary` but is wrapped with the extended types system.
4-
It's the native type and is unique to PowerShell itself.
3+
HashTable is essentially `System.Collections.HashTable`, the non-generic version of `Dictionary<,>`.
54

65
```ps1
76
@{} -is [System.Collections.IDictionary] # True
7+
@{} -is [System.Collections.HashTable] # True
88
```
99

10+
> [!TIP]
11+
> HashTable serves as more like a dictionary with syntax sugar, if you want it to be more like object literal, use `[pscustomobject]`.
12+
1013
## Creation
1114

1215
PowerShell has builtin syntax for creating a HashTable.
@@ -17,6 +20,7 @@ $foo = @{
1720
Name = 'foo'
1821
Age = 18
1922
}
23+
2024
$foo = @{ Name = 'foo'; Age = 18 }
2125
```
2226

@@ -29,13 +33,13 @@ $foo = @{ Name = 'foo'; Age = 18 }
2933
C = 'C'
3034
B = 'B'
3135
A = 'A'
32-
}).Keys # C B A
36+
}).Keys # C B A # [!code highlight]
3337
3438
@{
3539
C = 'C'
3640
B = 'B'
3741
A = 'A'
38-
}.Keys # B C A
42+
}.Keys # B C A # [!code highlight]
3943
```
4044

4145
> [!NOTE]
@@ -47,6 +51,7 @@ $foo = @{ Name = 'foo'; Age = 18 }
4751
## Access Values
4852
4953
You can access value of one or more keys by indexer.
54+
5055
```ps1
5156
$foo['Name'] # foo
5257
$foo['Name', 'Age'] # @('foo', 18)
@@ -56,11 +61,17 @@ $foo['Name', 'Age'] # @('foo', 18)
5661

5762
```ps1
5863
$foo.Name # foo
64+
$foo.'Name'
65+
$name = 'name'
66+
$foo.$name
5967
```
6068

6169
> [!TIP]
6270
> Always use indexer to access value of a HashTable. `.` will prefer Extended Property that might be unexpected.
6371
72+
> [!NOTE]
73+
> Key in HashTable can be any type. So if the key is not a singular type, you'll have to extract the key from `Keys` first and then access the value by the key.
74+
6475
## Merging
6576

6677
```ps1

docs/document/Powershell/docs/Language/PSCustomObject.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# PSCustomObject
22

3-
**PSCustomObject** is similar to object literal that can even have custom methods.
3+
Differ from HashTable, `[PSCustomObject]` is a representation for object literal.
4+
It
45

5-
## Create a PSCustomObject
6+
## Creation
67

78
PSCustomObject borrows the syntax from HashTable with a casting.
89

@@ -13,7 +14,7 @@ $john = [PSCustomObject] @{
1314
}
1415
```
1516

16-
### From a HashTable
17+
### From HashTable
1718

1819
You can explicitly convert a HashTable to PSCustomObject
1920

@@ -25,7 +26,7 @@ $table = @{
2526
$obj = [PSCustomObject]$table # [!code highlight]
2627
```
2728

28-
Or use `New-Object`
29+
Or use `New-Object`, but this might be slower.
2930

3031
```ps1
3132
$obj = New-Object -TypeName PSObject -Property $table

docs/document/Powershell/docs/Language/Script Block.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
**Script Block** is a special object in powerhsell, it looks like a syntax that creates a new scope in the flow, but itself can be stored in a variable.
44
So it's more like a anonymous function, you may call it as lambda expression.
55

6+
## Creation
7+
68
```ps1
79
$action = {
810
echo 'hello from script block'
@@ -14,6 +16,13 @@ $func = {
1416
}
1517
```
1618

19+
### From String
20+
21+
```ps1
22+
$script = [scriptblock]::Create('echo hello')
23+
& $script # hello
24+
```
25+
1726
## Invoke a Script Block
1827

1928
```ps1
@@ -24,11 +33,14 @@ $func = {
2433
& { param($a, $b) $a, $b } 'a' 'b'
2534
# a
2635
# b
36+
37+
# store result to variable
38+
$arr = & { param($a, $b) $a, $b } 'a' 'b'
2739
```
2840

2941
## Script Block as Lambda
3042

31-
As a replacement for lambda expression, Script Block is being used to work with LINQ api.
43+
As a replacement for lambda expression.
3244

3345
```ps1
3446
$arr = @(1, 2, 3, 5)

docs/document/Powershell/docs/Language/String.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@ Exception will be raised if conversion failed.
116116
'Janet is a girl' -replace '^Janet', 'Jane'
117117
```
118118

119+
All previous matches and captured groups can be accessed in `$matches` builtin variable.
120+
`$matches` is a HashTable, you can access named capture group by group name and use index for unamed group.
121+
122+
```ps1
123+
if ('John Smith' -match '^(?<FirstName>\b\w+\b) (\b\w+\b)$') {
124+
$matches.FirstName # John
125+
$matches[0] # John Smith
126+
$matches[1] # Smith
127+
}
128+
```
129+
119130
## Format String
120131

121132
Template string syntax is the same as `C#`.

0 commit comments

Comments
 (0)