Skip to content

Commit 20d18df

Browse files
committed
main
1 parent 3c4b7aa commit 20d18df

File tree

12 files changed

+256
-9
lines changed

12 files changed

+256
-9
lines changed

docs/document/Powershell/docs/Alias.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@
66
New-Alias <alias> <cmd_name>
77
```
88

9+
> [!TIP]
10+
> Use `nal` alias for `New-Alias`.
11+
912
> [!note]
10-
> `New-Alias` throws when the alias already exist while `Set-Alias` will overrides it.
13+
> `New-Alias` throws when the alias already exist while `Set-Alias` will override it.
1114
1215
## Override an Alias
1316

1417
```ps1
1518
Set-Alias <alias> <cmd_name>
1619
```
1720

21+
> [!TIP]
22+
> Use `sal` alias for `Set-Alias`.
23+
1824
## Checkout Alias
1925

2026
- Check out by alias
@@ -23,6 +29,9 @@ Set-Alias <alias> <cmd_name>
2329
Get-Alias <alias>
2430
```
2531

32+
> [!TIP]
33+
> Use `gal` alias for `Get-Alias`.
34+
2635
Or use `Get-Command`
2736

2837
```ps1
@@ -61,6 +70,10 @@ alias gl='git log' # fine
6170
New-Alias gl 'git log' # not allowed! # [!code error]
6271
```
6372

64-
> [!TIP]
65-
> Use functions to do alias with predetermined parameters in your profile.
73+
Should use function instead.
6674

75+
```ps1
76+
function gl {
77+
git log
78+
}
79+
```

docs/document/Powershell/docs/File System/2.Working Directory.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ There's two ways to get current working directory in Powershell.
2727
- `pwd` is an builtin alias for `Get-Location`.
2828

2929
```ps1
30-
(Get-Location).Path # equivalent to $pwd
30+
(Get-Location).Path # equivalent to $pwd.Path
3131
```
3232

3333
```console
File renamed without changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Operators
2+
3+
## Keyword Operator
4+
5+
| Desc | Operator | Example | Reference |
6+
| --------------- | --------------- | --------------- |--------------- |
7+
| Split a string by separator into a `string[]`| `-split` | `'1,2,3' -split ','` | [about_Split](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_split?view=powershell-7.4) |
8+
| Item1.2 | Item2.2 | Item3.2 | |
9+

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

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

33
## String Interpolation
44

5+
Interpolated string should quote with `"`.
6+
7+
Use `$` to interpolate:
8+
- variable
9+
- expression
10+
- member accessing
11+
12+
```ps1
13+
"Profile Path: $Profile" # variable
14+
"1 + 1 = $(1 + 1)" # expression
15+
16+
# functionaly identical
17+
"cwd: $($pwd.Path)" # Member accessing
18+
"cwd: $pwd"
19+
20+
"$(@(1, 2, 3)[0])"
21+
```
22+
23+
> [!NOTE]
24+
> `$()` is being called **SubExpression Operator** in Powershell.
25+
26+
## Verbatim String
27+
28+
Verbatim string should quote with `'`.
29+
Can contains new lines and
30+
31+
```ps1
32+
'
33+
It''s a verbatim string!
34+
and this is a new line!
35+
'
36+
```
37+
38+
> [!NOTE]
39+
> Verbatim string does not allow interpolation in Powershell which differs from `C#`.
40+
541
## Raw String
642

43+
> [!NOTE]
44+
> Raw string is being called **Here String** in Powershell.
45+
46+
**Here String** typically allows string contents starting at the second line and truncates the last newline.
47+
48+
Use `@'...'@` or `@"..."@` to present Verbatim Here String or Interpolated Here String.
49+
50+
```ps1
51+
@"
52+
<foo>$Profile</foo>
53+
"@
54+
55+
@'
56+
{
57+
"foo": {
58+
"bar": []
59+
}
60+
}
61+
'@
62+
```
63+
64+
> [!NOTE]
65+
> You can have quotation mark `"` in `@"..."@` and for `'` respectively.
66+
767
## String Concatenation
868

69+
Use `+`
70+
971
## Character Escaping
72+
73+
### Quotation Mark
74+
75+
Use double `"` to escape `"` in a interpolated string.
76+
77+
```ps1
78+
"""""" # ""
79+
"I quote, ""haha!""" # I quote, "haha!"
80+
```
81+
82+
Or use `` ` `` to escape the same quotation mark in the containing quotation.
83+
84+
```ps1
85+
"I quote, `"haha!`""
86+
```
87+
88+
Use double `'` to escape `'` in a verbatim string.
89+
90+
```ps1
91+
'''''' # ''
92+
'I quote, ''haha!''' # I quote, 'haha!'
93+
```
94+
95+
> [!NOTE]
96+
> See [about_Specical_Characters](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters?view=powershell-7.4)
97+
98+
## Split & Join
99+
100+
## Format String
101+
102+
## String Evaluation in Interpolation
103+
104+
### Collection
105+
106+
Collections like Array and HashTable are formatted by certain separator defined by `$OFS`(Output Field Separator)
107+
108+
```ps1
109+
"$(1, 2, 3)" # 1 2 3
110+
```
111+
112+
> [!NOTE]
113+
> `$OFS` is not a builtin variable, you'll have to create it manually or pwsh uses space ` ` as the separator.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Variable
2+
3+
## Declaration & Reference
4+
5+
Variable declaration and variable referencing shares a identical syntax.
6+
7+
Variable name must starts with `$` but `$` is not part of the name.
8+
9+
```ps1
10+
$var = 'foo'
11+
echo $var
12+
```
13+
14+
## Scope

docs/document/Powershell/docs/Object Manipulation/1.Overview.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ Powershell is a Object-Oriented shell language, so inspection and manipulation o
44

55
This section focuses on how to work with objects, including:
66

7-
- LINQ like transformation over objects.(including usage of `.NET` LINQ in Powershell)
87
- Object comparison.
9-
- Serialization and Deserialization.
8+
- LINQ like transformation over objects.(including usage of `.NET` LINQ in Powershell)
9+
- Understanding `PSObject`, ETS(Extended Type System), member manipulation and inspection.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Extended Type System
2+
3+
Extended Type System(ETS) is for consistent experience with **some** `.NET` types when working with Powershell.
4+
5+
For this purpose, Powershell wraps the traget object as `PSObject` with two approaches:
6+
7+
- Set the object to be wrapped as a meta object(similar to lua metatable)
8+
- Add extra members to the containing `PSObject` itself.
9+
10+
A common example would be `Process` type.
11+
12+
```ps1
13+
(gps | select -First 1).GetType().Name # Process
14+
(gps | select -First 1) -is [PSObject] # True
15+
(gps | select -First 1) -is [PSCustomObject] # True
16+
# -is checks underlying type too
17+
(gps | select -First 1) -is [System.Diagnostics.Process] # True
18+
```
19+
20+
The functionality of ETS achieved benifits:
21+
22+
- Allow custom behaviours like formatting, sorting for your custom type or object.
23+
- Dynamic interpreting a `HashTable` to any `PSObject`
24+
- Accessibility to the underlying object.
25+
- Manipulation on extended members.
26+
27+
## ETS Members
28+
29+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# ForEach
2+
3+
> [!TIP]
4+
> Use `foreach` or `%` alias for `ForEach-Object`.
5+
6+
## Works like Select
7+
8+
`ForEach-Object` can do the same thing as `Select-Object -ExpandProperty`.
9+
10+
```ps1
11+
gci | foreach Name
12+
# or
13+
gci | foreach { $_.Name }
14+
# equivalent to
15+
gci | select -ExpandProperty Name
16+
```
17+
18+
The way `ForEach-Object` behaves is collecting implicitly returned values as an array. Every implicitly returned value will be collected as a item.
19+
20+
```ps1
21+
gci | foreach { $_.Exists, $false } # True, False, True, False...
22+
```
23+
24+
If you do want a `$null` return, use `Out-Null` to swallow the value.
25+
26+
```ps1
27+
# doesn't make much sense though
28+
(gci | foreach { $_.Name | Out-Null }) -eq $null # True
29+
```
30+

docs/document/Powershell/docs/Object Manipulation/Member Types.md renamed to docs/document/Powershell/docs/Object Manipulation/Object Creation.md

File renamed without changes.

0 commit comments

Comments
 (0)