Skip to content

Commit c0e7fdd

Browse files
committed
main
1 parent 6259b13 commit c0e7fdd

File tree

11 files changed

+271
-30
lines changed

11 files changed

+271
-30
lines changed

docs/document/PowerShell/docs/File System/1.Overview.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

docs/document/PowerShell/docs/File System/3.Special Folders.md

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Path
2+
3+
## Resolve Path
4+
5+
- `-Path(0, pv)`: expand path containing special characters like `~` or UNC(Universal Naming Convention) path and some other special paths
6+
```ps1
7+
Resolve-Path '~/*' # returns all PathInfo of children under `~`
8+
Resolve-Path ~ # returns expanded path as PathInfo of `~`
9+
```
10+
- `-Relative(switch)`: get all relative paths from current path or specified location of items to be listed
11+
```ps1
12+
Resolve-Path '~/*' -Relative # list all relative string of children under ~ relative to current location
13+
```
14+
- `-RelativeBasePath`: get all relative paths from specified location of items to be listed
15+
```ps1
16+
Resolve-Path '~/*' -RelativeBasePath ~ -Relative # list all children of ~ relative to ~ itself
17+
```
18+
19+
## Part of Path
20+
21+
- `-Path(0, pv)`: get parent path as `PathInfo`
22+
```ps1
23+
Split-Path ([System.IO.Path]::GetTempFileName()) # C:\Users\username\AppData\Local\Temp
24+
```
25+
- `-Parent(!, switch)`: optional, the default flag for `Split-Path`, returns the parent container of `-Path`
26+
- `-Leaf(switch)`: get base name of a path
27+
```ps1
28+
Split-Path ([System.IO.Path]::GetTempFileName()) -Leaf # tmpfigvir.tmp
29+
```
30+
- `-LeafBase(switch)`: get base name without extension
31+
```ps1
32+
Split-Path ([System.IO.Path]::GetTempFileName()) -LeafBase # tmpfigvir
33+
```
34+
- `-Extension(switch)`: get extension
35+
```ps1
36+
Split-Path ([System.IO.Path]::GetTempFileName()) -Extension # .tmp
37+
```
38+
- `-Qualifier(switch)`: get drive prefix of the path, not available on non-Windows platform
39+
```ps1
40+
Split-Path ([System.IO.Path]::GetTempFileName()) -Qualifier # C:
41+
```
42+
- `-NoQualifier(switch)`: exclude the drive prefix of the path, not available on non-Windows platform
43+
```ps1
44+
Split-Path ([System.IO.Path]::GetTempFileName()) -NoQualifier # \Users\username\AppData\Local\Temp\tmpoo33lr.tmp
45+
```
46+
- `-IsAbsolute(switch)`: telling whether the path is absolute
47+
- `-Resolve(switch)`: handles relative or wildcard path for `-Path`
48+
```ps1
49+
Split-Path ~/* -Resolve # all parents of children under ~, which are all the same as `Resolve-Path ~`
50+
```
51+
52+
## Path Validation
53+
54+
- `-Path(0, pv)`: telling whether the path exists, can be relative
55+
```ps1
56+
Test-Path '~/.vimrc'
57+
Test-Path '../.gitignore'
58+
```
59+
- `-Filter`: common filter
60+
```ps1
61+
Test-Path '~/*rc'
62+
```
63+
- `-IsValid(switch)`: telling whether the provided path has correct syntax
64+
```ps1
65+
Test-Path 'foo/bar' # False
66+
Test-Path 'foo/bar' -IsValid # True
67+
```
68+
- `-Exclude` & `-Include`: telling whether any/no path matches the specified pattern
69+
```ps1
70+
Test-Path '~/*' -Exclude *rc # is there any file exists besides rc files?
71+
```
72+
- `-PathType`: telling whether the path is a leaf or a container(leaf is the child of a container)
73+
```ps1
74+
Test-Path '~/.vimrc' -PathType Leaf # True, it is a file indeed
75+
```
76+
> [!NOTE]
77+
> The meaning of `Leaf` is not the same as `Leaf` in `Split-Path`, `Leaf` can be any kind of child of a container in `Split-Path`.
78+
- `-NewerThan` & `-OlderThan`: telling whether the path was created before/after a date(can be date string)
79+
```ps1
80+
Test-Path ~/.vimrc -NewerThan "2012.6.12"
81+
```
82+
83+
## Join Path
84+
85+
- `-Path(0)` & `-ChildPath(1)`: join one or more parents with single child
86+
```ps1
87+
Join-Path C:, D: foo
88+
# C:\foo
89+
# D:\foo
90+
```
91+
- `-AdditionalChildPath(remain)`: accepts unlimited count of child paths
92+
```ps1
93+
Join-Path -Path foo -ChildPath foo -AdditionalChildPath foo, foo, foo # this is how it work formally
94+
Join-Path foo foo foo foo foo # this is how you could use it in daily life
95+
```
96+
- `-Resolve(switch)`: resolve( and validate) the path and join them, supports wildcard to join multiple matches at one time
97+
```ps1
98+
Join-Path ~/Projects nix-config -Resolve # C:\Users\username\Projects\nix-config
99+
Join-Path ~/Projects .git -Resolve # C:\Users\username\Projects\.git does not exist # [!code error]
100+
Join-Path ~/Projects * -Resolve # equvalent to `Resolve-Path ~/Projects/* | % Path` # [!code highlight]
101+
```

docs/document/PowerShell/docs/Language/Control Flow.md

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,128 @@ It has a few different patterns available:
105105
- Regex: matching by regex string, specifically for `string`.
106106
- Wildcard: matching by wildcard string, specifically for `string`.
107107

108+
### Synopsis
109+
110+
`switch` in PowerShell differs from c-like languages in:
111+
- condition can be a procedure(scriptblock) so you can perform more nested and complex determine
112+
```ps1
113+
switch (1) {
114+
{ $_ -is [int] } { "Int32" }
115+
}
116+
```
117+
- can have implicit return, each time the case matches yields the result into the final array, or just the singular result when only one case matches
118+
```ps1
119+
$foo = switch ($bar) { <# ... #> }
120+
$foo -is [System.Object[]] # True
121+
```
122+
- `default` block is not required(`$null` will be returned when no case is matched)
123+
- can match for not only a singular object but an collection
124+
```ps1
125+
$foo = switch (1, 2, 3) { }
126+
```
127+
- use `continue` to skip current enumeration
128+
```ps1
129+
switch (1, 2) {
130+
1 { continue } # I don't want to preceed with this value 1, next!
131+
2 { "aha" }
132+
} # aha
133+
```
134+
135+
There's three options available `switch`(specifically for `string` macthing):
136+
- `-Exact`: the default option that matches the string by literal, can be elided
137+
- `-Regex`: match by regex condition
138+
- `-Wildcard`: match by wildcard condition
139+
- `-CaseSensetive`: case-sensitive matching, can be combined with any of other three options
140+
108141
### Constant Pattern
109142
143+
Constant pattern is specifically for numbers and strings.
144+
145+
While `switch` has dedicated options for strings but those does not apply on other literals.
146+
However, when a non-string type is tried to match the string case by no matter which option you specified, it will be evaluated to string.
147+
148+
```ps1
149+
$foo = 1
150+
$bar = switch -Exact ($foo) {
151+
1 { "one" }
152+
2 { "two" }
153+
3 { "three" }
154+
1 { "one" } # reachable # [!code warning]
155+
'1' { "stringified one" } # matched too! # [!code highlight]
156+
}
157+
158+
$bar = switch ($foo) { # -Exact can be elided # [!code highlight]
159+
1 { "one"; break }
160+
2 { "two" }
161+
3 { "three" }
162+
1 { "one" } # not reachable # [!code highlight]
163+
default { "ok I am the last resort" }
164+
}
165+
```
166+
110167
### Type Pattern
111168

112-
### Regex Pattern
169+
Nothing else
170+
171+
```ps1
172+
switch ("foo") {
173+
{ $_ -is [string] -and $_.Length -gt 0 } { 'all right' }
174+
}
175+
```
176+
177+
### Regex & Wildcard Pattern
178+
179+
I don't have much to say
180+
181+
```ps1
182+
switch -Regex -CaseSensetive ("hello") {
183+
"h\d+" { "..." }
184+
"H\w+" { "..." }
185+
}
186+
187+
```
188+
189+
## Trap
113190

114-
### Wildcard Pattern
191+
`trap` is been called as a statement in PowerShell worlatchedd, but this is more like a `trap` block, for it:
192+
- serve as a isolated process for handling any error raised in the script or function
193+
- can be defined anywhere inside the script or function
115194

195+
But what makes it different from *block* is, you can specify multiple `trap` for different error types.
196+
**And PowerShell only run the most specific `trap` statement for the error**
116197

198+
> [!NOTE]
199+
> Use `$_` to access the captured error object inside `trap`
200+
201+
> [!WARNING]
202+
> Do not defined more than one `trap` for a same error type, only the first would be executed
203+
204+
```ps1
205+
param()
206+
207+
trap {
208+
"Any error was triggered"
209+
}
210+
211+
trap [System.StackOverflowException] {
212+
"A dedicated exception happened"
213+
}
214+
```
215+
216+
`trap` does not break the process, but only executes when particular error happens unless, you use `break`.
217+
218+
```ps1
219+
param()
220+
221+
trap {
222+
"Stop here"
223+
break
224+
}
225+
226+
ErrorHere
227+
228+
Write-Host "Not reachable here" # [!code warning]
229+
```
230+
231+
> [!NOTE]
232+
> `continue` is also available in `trap`, the only difference is it does not write the error to error stream
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Stream Redirection
2+
3+
The streams are implicitly working everyday we use redirection operator `>`, `>>` in PowerShell.
4+
Streams are `Success`, `Error`, `Warning` ... that's exactly the counterpart of `Write-Output`, `Write-Error` ...
5+
6+
> [!NOTE]
7+
> See: [Output Streams](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-7.4#redirectable-output-streams)
8+
9+
> [!NOTE]
10+
> Append redirection `>>` is only available for files
11+
12+
## Redirect to File
13+
14+
When you write to a file using `>` to redirect content, it's actually redirecting the `Success` stream to the file.
15+
16+
```ps1
17+
gci > foo.txt
18+
# equivalent to
19+
gci 1> foo.txt # 1 represents the Success stream
20+
```
21+
22+
## Redirect to Another Stream
23+
24+
```ps1
25+
gci 1>&2 # redirect Success stream to Error stream
26+
```
27+
28+
## Redirect at One Go
29+
30+
You can apply multiple redirection for a same source.
31+
32+
```ps1
33+
& { Write-Warning "hello"; Write-Error "hello"; Write-Output "hi" } 3>&1 2>&1 > C:\Temp\redirection.log
34+
```
35+
36+
## Suppress Message from Streams
37+
38+
If you only care specific message from streams or exculde particular stream, redirect it to `$null`
39+
40+
```ps1
41+
ffmpeg -h *> $null # swollow all streams output from ffmpeg
42+
```

docs/document/PowerShell/docs/Terminology.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,13 @@ By this approach, you can expose minimal privileges and custom utilities to user
2323
Each Runspace creates isolated session state just like a normal powershell instance.
2424

2525
A normal powershell can be referred as a Runspace too.
26+
27+
## Execution Policy
28+
29+
Execution Policy is dedicated for Windows platform, it restricts the authority to execute a valid PowerShell script or command.
30+
31+
- `AllSigned`: requires scripts to be signed by trusted-publisher
32+
- `Bypass`: does not block anything
33+
- `Default`: `Restricted` for Windows personal and `RemoteSigned` for Windows Server
34+
- `Unrestricted`: default and unchangeable for non-windows platform
35+
- `Restricted`: no script allowed; command execution allowed

0 commit comments

Comments
 (0)