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/HashTable.md
+61Lines changed: 61 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,66 @@
1
1
# HashTable
2
2
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.
5
+
6
+
```ps1
7
+
@{} -is [System.Collections.IDictionary] # True
8
+
```
9
+
10
+
## Creation
11
+
12
+
PowerShell has builtin syntax for creating a HashTable.
13
+
Inline declaration requires `;` to distinguish key-value pairs.
14
+
15
+
```ps1
16
+
$foo = @{
17
+
Name = 'foo'
18
+
Age = 18
19
+
}
20
+
$foo = @{ Name = 'foo'; Age = 18 }
21
+
```
22
+
23
+
### Ordered HashTable
24
+
25
+
`[ordered]` is a mark can be used when creating HashTable literal, it makes sure that all entries are ordered as in declaration and subsequent appending.
26
+
27
+
```ps1
28
+
([ordered]@{
29
+
C = 'C'
30
+
B = 'B'
31
+
A = 'A'
32
+
}).Keys # C B A
33
+
34
+
@{
35
+
C = 'C'
36
+
B = 'B'
37
+
A = 'A'
38
+
}.Keys # B C A
39
+
```
40
+
41
+
> [!NOTE]
42
+
> `[ordered]` can not be used as type, but it's indeed a `System.Collections.Specialized.OrderedDictionary`.
Copy file name to clipboardExpand all lines: docs/document/Powershell/docs/Language/String.md
+30Lines changed: 30 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -91,10 +91,40 @@ Use double `'` to escape `'` in a verbatim string.
91
91
> [!NOTE]
92
92
> See [about_Specical_Characters](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters?view=powershell-7.4)
93
93
94
+
## Arithmetic with Numerics
95
+
96
+
Powershell will try to convert the string on the right operand to the same type as left operand.
97
+
Exception will be raised if conversion failed.
98
+
99
+
```ps1
100
+
1 + '2' # 3
101
+
'2' + 1 # '21'
102
+
[DateTime]::Now + '00:00:15:00' # adds 15 minutes
103
+
```
104
+
94
105
## Split & Join
95
106
107
+
```ps1
108
+
'1,2,3' -split ',' # 1 2 3 as strings
109
+
(gci -file) -join ',' # ToString is invoked to evaluated objects to string.
110
+
```
111
+
112
+
## Match & Replace
113
+
114
+
```ps1
115
+
'Janet is a girl' -match 'Jane' # True
116
+
'Janet is a girl' -replace '^Janet', 'Jane'
117
+
```
118
+
96
119
## Format String
97
120
121
+
Template string syntax is the same as `C#`.
122
+
Standard numeric format like `:C`, `:X` are supported.
Copy file name to clipboardExpand all lines: docs/document/Powershell/docs/Understanding Pipeline.md
+17-10Lines changed: 17 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ Overview of pipeline in powershell:
10
10
11
11
12
12
13
-
## How Cmdlet Accept Pipeline Input
13
+
## Pipeline Input Strategy
14
14
15
15
There's two solution when a pipeline input comes in as a fallback:
16
16
@@ -33,7 +33,7 @@ gci -File | spps
33
33
ByValue is always tried first, and then use ByPropertyName, or it finally throws.
34
34
A parameter accepts pipeline input does not necessarily have both solutions, it can have at least one of them.
35
35
36
-
## How PowerShell Enumerate Pipeline Input
36
+
## Pipeline Input as Enumerator
37
37
38
38
As we know, PowerShell can handle multiple objects from an enumerator from object that implements `IEnumerable` or `IEnumerable<T>`, or even duck typed with `GetEnumerator`.
39
39
@@ -43,28 +43,33 @@ While for types that are not linear collection, manually invoking `GetEnumerator
43
43
- HashTable has dynamic typing so we can't presume a uniformed calculation for our cmdlet
44
44
-`string` is `IEnumerable` but we surely don't expect the auto enumeration.
45
45
46
+
This is simply because these types are more likely to be treated as a whole object, even when dictionaries are `IEnumerable<KeyValuePair<,>>`.
This is simply because these types are more likely to be treated as a whole object, even when dictionaries are `IEnumerable<KeyValuePair<,>>`.
53
-
54
-
55
54
## Enumerate Pipeline Items
56
55
57
-
You can use `$input` to refer to the enumerator passed to the function. This is another way to access pipeline input items but with more control.
56
+
You can use `$input` to refer to the enumerator passed to the function. This is one way to access pipeline input items but with more control.
58
57
Another option is use `$_` to represent the current item in `process` block, this is way more limited but commonly used.
59
58
59
+
> [!NOTE]
60
+
> `$_` and `$input` are isolated, they don't affects each other.
61
+
60
62
-`$input` represents a enumerator for pipeline input in `process` block.
61
63
-`$input` represents the whole collection for pipeline input in `end` block.
62
64
-`$input` will be consumed after being used once in either `process` or `end`. Use `Reset` to get it back.
63
65
- You can't use `$input` in both `process` and `end`.
64
66
65
67
### Access Current Item
66
68
67
-
`$input.Current` have to manually invoke `MoveNext` before you access `Current` in `process` block since it's not a `while` loop.
69
+
> We're not going to talk about `$_`, it's fairly simple. All the quirks is about `$input`.
70
+
71
+
`$input.Current` is `$null` by default, you'll have to manually invoke `MoveNext` before you access `Current` in `process` block since it's not a `while` loop.
72
+
68
73
69
74
```ps1
70
75
function Test {
@@ -122,7 +127,7 @@ gci -file | Test
122
127
If you write a custom function that have one or more parameters accept pipeline input, what is going on inside?
123
128
124
129
- In `begin` block, there's no value assigned to each `ByPropertyName` parameter, they remain default.
125
-
- In `process` block, each
130
+
- In `process` block, each `ByPropertyName` parameter represents the current property value extracted from the current pipeline input object.
126
131
127
132
```ps1
128
133
function Foo {
@@ -139,11 +144,13 @@ function Foo {
139
144
}
140
145
141
146
process {
142
-
$Name
143
-
$Length
147
+
$Name # Name of current pipeline item
148
+
$Length # Length of current pipeline item
144
149
}
145
150
}
146
151
147
152
gci -file | Foo
148
153
```
149
154
155
+
> [!TIP]
156
+
> `ByPropertyName` parameter can also be a array type, it all depends the implementation you want, it behaves the same.
0 commit comments