Skip to content

Commit d934c54

Browse files
committed
main
1 parent 6715edb commit d934c54

File tree

15 files changed

+261
-36
lines changed

15 files changed

+261
-36
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Units of Size
2+
3+
Powershell has builtin suffix for some numeric types that can represent file size and memory size like `mb`, `gb` and so on.
4+
Powershell recognized the units and convert it into the base representation of size which is `byte`.
5+
So it's like some implicit operators in `C++` that do the job for us.
6+
7+
- **NOT** case-sensitive
8+
- Auto converts into bytes(the default unit)
9+
- No concrete or wrapper type exists, just a annotation.
10+
11+
12+
```ps1
13+
gci | where { $_.Length > 1kb }
14+
```

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,110 @@
22

33
## Creation
44

5+
```ps1
6+
$foo = 1,2,3
7+
$foo = @(1,2,3)
8+
9+
$foo = ,1 # with only one item
10+
11+
$foo = 1..10 # from inclusive range 1 to 10
12+
13+
$foo = @() # empty array
14+
```
15+
516
> [!NOTE]
617
> The default type of a array literal is `object[]`, you can annotate with a type.
718
> ```ps1
819
> [int[]]$foo = 1, 2, 3
920
> ```
21+
22+
> [!TIP]
23+
> Prefer `,...` to create an array with single element, it has less ambiguity.
24+
25+
> [!NOTE]
26+
> Things like `1,2,3` itself is an expression, you can capture them as an object.
27+
> But it cannot spread out items except a weird `,(1,2,3)`.
28+
>```ps1
29+
>(1,2,3).Length # 3
30+
>
31+
>(,(1,2,3)).Length # 3 # does spread items # [!code highlight]
32+
>
33+
>(,@(1,2,3)).Length # 1 # does not spread items # [!code highlight]
34+
>
35+
>((gci), (gci ..)).Length # 2 # [!code highlight]
36+
> ```
37+
38+
### Collect from Expressions
39+
40+
`@()` actually collects from expressions, and all items from expressions will be flattened into a whole array.
41+
So it can be another way to spread out arrays into one.
42+
43+
- Use `;` to separate expressions for flatten arrays from them.
44+
- Use `,` to separate expressions if you want them to be sub-array.
45+
46+
```ps1
47+
@((1, 2, 3), (1, 2, 3)).Length # 2
48+
@((1, 2, 3); (1, 2, 3)).Length # 6
49+
50+
@((ls), (ls ..)).Length # 2
51+
@(ls; ls ..).Length # > 0
52+
```
53+
54+
## Access an Item
55+
56+
Powershell allows
57+
58+
## Concatenation
59+
60+
Generates new array from two concatenated.
61+
62+
```ps1
63+
((1,2,3) + (1,2,3)).Length # 6
64+
```
65+
66+
## Repetition
67+
68+
Use `*` to repeat the array content for certain times.
69+
70+
```ps1
71+
((1,2,3) * 3).Length # 9
72+
```
73+
74+
## Slicing
75+
76+
Use range operator to slice an array.
77+
78+
```ps1
79+
(1..10)[0..5] # 1 to 6
80+
81+
(1..10)[-3..-1] # 8 to 10, last 3 items
82+
83+
# Reversed slicing
84+
(1..10)[-1..-3] # 10, 9, 8; last 3 items in a reversed order
85+
86+
((1..10)[-1..-1]) -is [Array] # True, slicing always returns array
87+
```
88+
89+
> [!NOTE]
90+
> Differ from `C#`, range operator in Powershell is inclusive from both sides.
91+
92+
### Range Unions
93+
94+
You can specify multiple ranges for slicing with `+`, selected item will be collected together.
95+
Separate different ranges by `+` to generate a range union.
96+
97+
> [!NOTE]
98+
> A range can be a single index or simple range
99+
100+
```ps1
101+
# Select 1 to 3, 5 to 6, and a single 8
102+
(1..10)[0..2+4..5+7]
103+
```
104+
105+
## Multi-Dim Array
106+
107+
You'll have to create Multi-Dim array from .NET type constructor only.
108+
109+
```ps1
110+
$foo = [int[,]]::New(2, 2) # 2 * 2 array
111+
```

docs/document/Powershell/docs/Language/Collection.md

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Control Flow
2+
3+
## Falsy Values
4+
5+
- `$false`
6+
- `$null`
7+
- Empty string
8+
- Numeric zeros
9+
- Empty collections implemented `IList`.
10+
11+
> [!NOTE]
12+
> You can cast falsy values to boolean.
13+
>```ps1
14+
>[bool]@() # False
15+
>```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# HashTable
2+
3+
## Merging
4+
5+
```ps1
6+
@{ foo = 123; bar = '123' } + @{ baz = 234 }
7+
```
8+
9+
> [!WARNING]
10+
> `+` will throw if the two have any duplicated key.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Keyword Operators
2+

docs/document/Powershell/docs/Language/Operators.md

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Scope
2+
3+
Variables and functions can have a explicit scope.
4+
5+
```ps1
6+
$<scope>:foo = 1
7+
function <scope>:Foo {}
8+
```
9+
10+
## Scope Modifier
11+
12+
- `global`: accessible for the whole session. The root parent scope in a runspace.
13+
- `local`: default. Accessible in current script or script block or function. Can be accessed by child scopes.
14+
- `private`: accessible for current scope, meaning it can't be accessed by any other scope.
15+
- `script`: only accessible in script module. The default scope in a
16+
- Scopes from PSProviders
17+
- `env`: environment variables
18+
- `alias`: alias
19+
- `function`
20+
- `variable`

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ Use `@'...'@` or `@"..."@` to present Verbatim Here String or Interpolated Here
6464
> [!NOTE]
6565
> You can have quotation mark `"` in `@"..."@` and for `'` respectively.
6666
67-
## String Concatenation
68-
69-
Use `+`
70-
7167
## Character Escaping
7268

7369
### Quotation Mark
@@ -99,6 +95,14 @@ Use double `'` to escape `'` in a verbatim string.
9995

10096
## Format String
10197

98+
## Repetition
99+
100+
Use `*` to repeat a string.
101+
102+
```ps1
103+
'abc' * 2 # abcabc
104+
```
105+
102106
## String Evaluation in Interpolation
103107

104108
### Collection

docs/document/Powershell/docs/Language/Variable.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ echo $var
1212
```
1313

1414
## Scope
15+
16+

0 commit comments

Comments
 (0)