Skip to content

Commit 3c4b7aa

Browse files
committed
grrr
1 parent b9a35ce commit 3c4b7aa

File tree

12 files changed

+283
-21
lines changed

12 files changed

+283
-21
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Overview
22

3+
- Integrated with `.NET`
4+
Reuse almost any type in `.NET`.
35
- Dynamic typing
6+
Powershell adds extra attribute and properties on dotnet types to enhance experience.
47
- Case insensitive
8+
All language syntax, pattern syntax and even strings are case-insensitive.
9+
(There's exception for file system on non-Windows platform)
510
- Everything is object, more than plain text in shell.
11+
Powershell formats the object value as a table if the object is not a primitive type.
12+
For primitive type, `Tostring()` will be used instead.
13+
- Always handle both array and singular object.
14+
A powershell cmdlet always accepts an array or an single object as input parameter.
15+
And returns the result as an array or an object too.

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

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

33
**FileSystem** is a builtin **PSProvider** in Powershell.
44

5-
FileSystem can have one or more **PSDrive** depends on the system.
5+
FileSystem can have one or more **PSDrive** depending on the system.
66

77
In windows, drives can be `C:`, `D:` and so on. In linux they can be some mount points.
88

9-
This is introduces how to manipulate file system objects with item-related cmdlet.
9+
This section introduces how to manipulate file system objects with item-related cmdlet.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Collection
2+
3+
## Array
4+
5+
## HashTable

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
11
# Function
22

3+
## Return
4+
5+
Powershell allows implicit return, and multiple implicit returns.
6+
7+
> [!NOTE]
8+
> Implicit returns are auto-collected as an array or single value.
9+
> And it does not print out anything.
10+
11+
```ps1
12+
[int] function Sum {
13+
param([int]$l, [int]$r)
14+
$l + $r # implicit return # [!code highlight]
15+
}
16+
17+
# You won't need to declare an array and append it on each loop!
18+
# they're collected automatically as they're implicit returns
19+
function Foo {
20+
for($i = 0; $i -lt 10; $i = $i + 1) {
21+
$i
22+
}
23+
}
24+
25+
(Foo).GetType().Name # object[] # [!code highlight]
26+
```
27+
28+
Explicit return is surely supported, but more like a necessity to exit inside a flow.
29+
30+
```ps1
31+
[int] function Sum {
32+
param([int]$l, [int]$r)
33+
return $l + $r # explicit return # [!code highlight]
34+
$r + $l # not reachable # [!code warning]
35+
}
36+
```
37+
338
## Parameter
439

540
Parameters are wrapped inside a function block with `param(...)`
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Invocation
2+
3+
> [!CAUTION]
4+
> Always be careful to run an external script or input script content.
5+
6+
## Call Operator
7+
8+
Call operator `&` can be used for invoking one of the following:
9+
10+
- Command without any option.
11+
- A `.ps1` script file
12+
- Script block
13+
14+
**`&` is awared of the context of current session, it does not start a new process.**
15+
16+
It's more like a system execution
17+
18+
```ps1
19+
& 'gps'
20+
& 'gps pwsh' # extra option not allowed # [!code error]
21+
& 'gps' pwsh # pass option after command name instead # [!code ++]
22+
& 'gps' -Name pwsh # pass option after command name instead # [!code ++]
23+
24+
& 'path/to/script.ps1' # may add some option... # [!code highlight]
25+
26+
& { param($a, $b) $a, $b } 1, 2
27+
```
28+
29+
## Invoke-Expression
30+
31+
`Invoke-Expression` used for executing any script content represented as a string.
32+
33+
```ps1
34+
Invoke-Expression 'gps pwsh'
35+
'gps pwsh' | iex
36+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# PSCustomObject
2+
3+
**PSCustomObject** is similar to object literal that can even have custom methods.
4+
5+
## Create a PSCustomObject
6+
7+
PSCustomObject borrows the syntax from HashTable with a casting.
8+
9+
```ps1
10+
$john = [PSCustomObject] @{
11+
Name = 'John Smith'
12+
Age = 18
13+
}
14+
```
15+
16+
### From a HashTable
17+
18+
You can explicitly convert a HashTable to PSCustomObject
19+
20+
```ps1
21+
$table = @{
22+
Foo = 'foo'
23+
}
24+
25+
$obj = [PSCustomObject]$table # [!code highlight]
26+
```
27+
28+
Or use `New-Object`
29+
30+
```ps1
31+
$obj = New-Object -TypeName PSObject -Property $table
32+
```
33+
34+
## Copy a PSCustomObject
35+
36+
- Shallow copy
37+
38+
```ps1
39+
$john = [PSCustomObject] @{
40+
Name = 'John Smith'
41+
Age = 18
42+
}
43+
44+
$smith = $john.PSObject.Copy()
45+
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Script Block
2+
3+
**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.
4+
So it's more like a anonymous function, you may call it as lambda expression.
5+
6+
```ps1
7+
$action = {
8+
echo 'hello from script block'
9+
}
10+
11+
$func = {
12+
param($foo, $bar)
13+
return "$foo$bar"
14+
}
15+
```
16+
17+
## Invoke a Script Block
18+
19+
```ps1
20+
& { 1, 2 }
21+
# 1
22+
# 2
23+
24+
& { param($a, $b) $a, $b } 'a' 'b'
25+
# a
26+
# b
27+
```
28+
29+
## Script Block as Lambda
30+
31+
As a replacement for lambda expression, Script Block is being used to work with LINQ api.
32+
33+
```ps1
34+
$arr = @(1, 2, 3, 5)
35+
$arr.Where({ $_ -lt 4 })
36+
```

docs/document/Powershell/docs/Modulization/Modules.md

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,3 @@ Search module to be installed in registered repositories.
2323
Find-Module <pattern>
2424
```
2525

26-
## Custom Module Path
27-
28-
Powershell automatically scans modules at certain locations.
29-
Inspect existing module path by:
30-
31-
:::code-group
32-
```ps1[Windows]
33-
$env:PSModulePath -split ';' # windows
34-
```
35-
```ps1[Linux/Mac]
36-
$env:PSModulePath -split ':' # linux/mac
37-
```
38-
:::
39-
40-
```ps1
41-
$env:PSModulePath += [IO.Path]::PathSeparator + '<path>'
42-
```

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

Lines changed: 1 addition & 1 deletion
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.
7+
- LINQ like transformation over objects.(including usage of `.NET` LINQ in Powershell)
88
- Object comparison.
99
- Serialization and Deserialization.

docs/document/Powershell/docs/Object Manipulation/Compare Objects.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Compare Objects
22

3+
## Overview
4+
35
> [!TIP]
46
> Use `diff` or `compare` alias for `Compare-Object`
57
@@ -46,4 +48,4 @@ diff @('abc', 'dfg') @('fg', 'abc') -Property Length # compare on string.Length
4648
- `ToString()` and compare on string
4749

4850
> [!TIP]
49-
> If the object to be compared doesn't implement `IComparable`, you should use `-Property`.
51+
> If the object to be compared doesn't implement `IComparable`, you should use `-Property` unless it's a primitive type.

0 commit comments

Comments
 (0)