-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
283 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
# Overview | ||
|
||
- Integrated with `.NET` | ||
Reuse almost any type in `.NET`. | ||
- Dynamic typing | ||
Powershell adds extra attribute and properties on dotnet types to enhance experience. | ||
- Case insensitive | ||
All language syntax, pattern syntax and even strings are case-insensitive. | ||
(There's exception for file system on non-Windows platform) | ||
- Everything is object, more than plain text in shell. | ||
Powershell formats the object value as a table if the object is not a primitive type. | ||
For primitive type, `Tostring()` will be used instead. | ||
- Always handle both array and singular object. | ||
A powershell cmdlet always accepts an array or an single object as input parameter. | ||
And returns the result as an array or an object too. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Collection | ||
|
||
## Array | ||
|
||
## HashTable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Invocation | ||
|
||
> [!CAUTION] | ||
> Always be careful to run an external script or input script content. | ||
## Call Operator | ||
|
||
Call operator `&` can be used for invoking one of the following: | ||
|
||
- Command without any option. | ||
- A `.ps1` script file | ||
- Script block | ||
|
||
**`&` is awared of the context of current session, it does not start a new process.** | ||
|
||
It's more like a system execution | ||
|
||
```ps1 | ||
& 'gps' | ||
& 'gps pwsh' # extra option not allowed # [!code error] | ||
& 'gps' pwsh # pass option after command name instead # [!code ++] | ||
& 'gps' -Name pwsh # pass option after command name instead # [!code ++] | ||
& 'path/to/script.ps1' # may add some option... # [!code highlight] | ||
& { param($a, $b) $a, $b } 1, 2 | ||
``` | ||
|
||
## Invoke-Expression | ||
|
||
`Invoke-Expression` used for executing any script content represented as a string. | ||
|
||
```ps1 | ||
Invoke-Expression 'gps pwsh' | ||
'gps pwsh' | iex | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# PSCustomObject | ||
|
||
**PSCustomObject** is similar to object literal that can even have custom methods. | ||
|
||
## Create a PSCustomObject | ||
|
||
PSCustomObject borrows the syntax from HashTable with a casting. | ||
|
||
```ps1 | ||
$john = [PSCustomObject] @{ | ||
Name = 'John Smith' | ||
Age = 18 | ||
} | ||
``` | ||
|
||
### From a HashTable | ||
|
||
You can explicitly convert a HashTable to PSCustomObject | ||
|
||
```ps1 | ||
$table = @{ | ||
Foo = 'foo' | ||
} | ||
$obj = [PSCustomObject]$table # [!code highlight] | ||
``` | ||
|
||
Or use `New-Object` | ||
|
||
```ps1 | ||
$obj = New-Object -TypeName PSObject -Property $table | ||
``` | ||
|
||
## Copy a PSCustomObject | ||
|
||
- Shallow copy | ||
|
||
```ps1 | ||
$john = [PSCustomObject] @{ | ||
Name = 'John Smith' | ||
Age = 18 | ||
} | ||
$smith = $john.PSObject.Copy() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Script Block | ||
|
||
**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. | ||
So it's more like a anonymous function, you may call it as lambda expression. | ||
|
||
```ps1 | ||
$action = { | ||
echo 'hello from script block' | ||
} | ||
$func = { | ||
param($foo, $bar) | ||
return "$foo$bar" | ||
} | ||
``` | ||
|
||
## Invoke a Script Block | ||
|
||
```ps1 | ||
& { 1, 2 } | ||
# 1 | ||
# 2 | ||
& { param($a, $b) $a, $b } 'a' 'b' | ||
# a | ||
# b | ||
``` | ||
|
||
## Script Block as Lambda | ||
|
||
As a replacement for lambda expression, Script Block is being used to work with LINQ api. | ||
|
||
```ps1 | ||
$arr = @(1, 2, 3, 5) | ||
$arr.Where({ $_ -lt 4 }) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
110 changes: 110 additions & 0 deletions
110
docs/document/Powershell/docs/Object Manipulation/Select.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,111 @@ | ||
# Select | ||
|
||
`Select-Object` is a combination of `Select`, `Take`, `TakeLast` in dotnet LINQ. | ||
It can be appiled on singular object and any collection. | ||
|
||
> [!TIP] | ||
> Use `select` alias for `Select-Object`. | ||
## Select as an Object | ||
|
||
`Select-Object` wraps selected properties as an `PSCustomObject` | ||
|
||
```ps1 | ||
gps | Select-Object -Property Name # gets an object[] instead of string[] | ||
(gps | select Name -First).GetType().Name # PSCustomObject | ||
``` | ||
|
||
You can select multiple properties: | ||
|
||
```console | ||
$ gps | select Name, Id -First 1 | gm | ||
|
||
TypeName: Selected.System.Diagnostics.Process | ||
|
||
Name MemberType Definition | ||
---- ---------- ---------- | ||
Equals Method bool Equals(System.Object obj) | ||
GetHashCode Method int GetHashCode() | ||
GetType Method type GetType() | ||
ToString Method string ToString() | ||
Id NoteProperty int Id=12588 | ||
Name NoteProperty string Name=ABDownloadManager | ||
``` | ||
|
||
### Nested Property and Custom Property | ||
|
||
`-Property` accepts a **Script Block** to calculate the **nested** property you'd like to select. | ||
|
||
```console | ||
$ gps | select Name, { $_.StartTime.DayOfWeek } -First 1 | ||
|
||
Name $_.StartTime.DayOfWeek | ||
---- ------------------------ | ||
ABDownloadManager Thursday | ||
``` | ||
|
||
You can even define a custom calculation with a HashTable shaped as: | ||
- `lable: string`: name of the property | ||
- `expression: ScriptBlock`: an procedure returns the calculated property value. | ||
|
||
```console | ||
$ gci -File | select Name, @{ label = 'Size(KB)'; expression = { $_.Length / 1KB } } | ||
|
||
Name Size(KB) | ||
---- -------- | ||
.gitignore 0.01 | ||
dotfiles.ps1 1.69 | ||
flake.lock 1.64 | ||
flake.nix 1.02 | ||
home.nix 0.63 | ||
install.ps1 1.49 | ||
make_vs.ps1 2.06 | ||
README.md 0.85 | ||
``` | ||
|
||
## Select Value Only | ||
|
||
To select value of a property instead of being wrapped as an object, use `-ExpandProperty`. | ||
The return type is still an `object[]` since there's no generic resolution on Powershell. | ||
But each memeber should be string indeed in the following snippet. | ||
|
||
```ps1 | ||
gps | select -ExpandProperty Name | ||
(gps | select -ExpandProperty Name).GetType().Name # object[] | ||
(gps | select -ExpandProperty Name -First 1) -is [string] # True | ||
``` | ||
|
||
> [!NOTE] | ||
> `-ExpandProperty` can only take one property. | ||
## Take a Count | ||
|
||
`Select-Object` can also take specific count of items from a collection, from start or end. | ||
|
||
```ps1 | ||
gps | select -First 5 | ||
gps | select -Last 5 | ||
``` | ||
|
||
## Skip a Count | ||
|
||
```ps1 | ||
gps | select -Skip 5 | ||
gps | select -SkipLast 5 | ||
``` | ||
|
||
## Cherry Pick | ||
|
||
```ps1 | ||
$dir = ls -Directory | ||
$dir | select -Index 1, ($dir.Length - 1) # Pick first and last item | ||
``` | ||
|
||
## Distinct | ||
|
||
```ps1 | ||
# might have duplicated entries since file extensions should ignore casing. | ||
ls | select Extension -Unique | ||
# list all extensions appeared in current directory | ||
ls | select Extension -Unique -CaseInsensitive | ||
``` |