Skip to content

Commit

Permalink
main
Browse files Browse the repository at this point in the history
  • Loading branch information
sharpchen committed Nov 20, 2024
1 parent 3c4b7aa commit 20d18df
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 9 deletions.
19 changes: 16 additions & 3 deletions docs/document/Powershell/docs/Alias.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@
New-Alias <alias> <cmd_name>
```

> [!TIP]
> Use `nal` alias for `New-Alias`.
> [!note]
> `New-Alias` throws when the alias already exist while `Set-Alias` will overrides it.
> `New-Alias` throws when the alias already exist while `Set-Alias` will override it.
## Override an Alias

```ps1
Set-Alias <alias> <cmd_name>
```

> [!TIP]
> Use `sal` alias for `Set-Alias`.
## Checkout Alias

- Check out by alias
Expand All @@ -23,6 +29,9 @@ Set-Alias <alias> <cmd_name>
Get-Alias <alias>
```

> [!TIP]
> Use `gal` alias for `Get-Alias`.
Or use `Get-Command`

```ps1
Expand Down Expand Up @@ -61,6 +70,10 @@ alias gl='git log' # fine
New-Alias gl 'git log' # not allowed! # [!code error]
```

> [!TIP]
> Use functions to do alias with predetermined parameters in your profile.
Should use function instead.

```ps1
function gl {
git log
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ There's two ways to get current working directory in Powershell.
- `pwd` is an builtin alias for `Get-Location`.

```ps1
(Get-Location).Path # equivalent to $pwd
(Get-Location).Path # equivalent to $pwd.Path
```

```console
Expand Down
9 changes: 9 additions & 0 deletions docs/document/Powershell/docs/Language/Operators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Operators

## Keyword Operator

| Desc | Operator | Example | Reference |
| --------------- | --------------- | --------------- |--------------- |
| 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) |
| Item1.2 | Item2.2 | Item3.2 | |

104 changes: 104 additions & 0 deletions docs/document/Powershell/docs/Language/String.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,112 @@

## String Interpolation

Interpolated string should quote with `"`.

Use `$` to interpolate:
- variable
- expression
- member accessing

```ps1
"Profile Path: $Profile" # variable
"1 + 1 = $(1 + 1)" # expression
# functionaly identical
"cwd: $($pwd.Path)" # Member accessing
"cwd: $pwd"
"$(@(1, 2, 3)[0])"
```

> [!NOTE]
> `$()` is being called **SubExpression Operator** in Powershell.
## Verbatim String

Verbatim string should quote with `'`.
Can contains new lines and

```ps1
'
It''s a verbatim string!
and this is a new line!
'
```

> [!NOTE]
> Verbatim string does not allow interpolation in Powershell which differs from `C#`.
## Raw String

> [!NOTE]
> Raw string is being called **Here String** in Powershell.
**Here String** typically allows string contents starting at the second line and truncates the last newline.

Use `@'...'@` or `@"..."@` to present Verbatim Here String or Interpolated Here String.

```ps1
@"
<foo>$Profile</foo>
"@
@'
{
"foo": {
"bar": []
}
}
'@
```

> [!NOTE]
> You can have quotation mark `"` in `@"..."@` and for `'` respectively.
## String Concatenation

Use `+`

## Character Escaping

### Quotation Mark

Use double `"` to escape `"` in a interpolated string.

```ps1
"""""" # ""
"I quote, ""haha!""" # I quote, "haha!"
```

Or use `` ` `` to escape the same quotation mark in the containing quotation.

```ps1
"I quote, `"haha!`""
```

Use double `'` to escape `'` in a verbatim string.

```ps1
'''''' # ''
'I quote, ''haha!''' # I quote, 'haha!'
```

> [!NOTE]
> See [about_Specical_Characters](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters?view=powershell-7.4)
## Split & Join

## Format String

## String Evaluation in Interpolation

### Collection

Collections like Array and HashTable are formatted by certain separator defined by `$OFS`(Output Field Separator)

```ps1
"$(1, 2, 3)" # 1 2 3
```

> [!NOTE]
> `$OFS` is not a builtin variable, you'll have to create it manually or pwsh uses space ` ` as the separator.
14 changes: 14 additions & 0 deletions docs/document/Powershell/docs/Language/Variable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Variable

## Declaration & Reference

Variable declaration and variable referencing shares a identical syntax.

Variable name must starts with `$` but `$` is not part of the name.

```ps1
$var = 'foo'
echo $var
```

## Scope
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ Powershell is a Object-Oriented shell language, so inspection and manipulation o

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

- LINQ like transformation over objects.(including usage of `.NET` LINQ in Powershell)
- Object comparison.
- Serialization and Deserialization.
- LINQ like transformation over objects.(including usage of `.NET` LINQ in Powershell)
- Understanding `PSObject`, ETS(Extended Type System), member manipulation and inspection.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Extended Type System

Extended Type System(ETS) is for consistent experience with **some** `.NET` types when working with Powershell.

For this purpose, Powershell wraps the traget object as `PSObject` with two approaches:

- Set the object to be wrapped as a meta object(similar to lua metatable)
- Add extra members to the containing `PSObject` itself.

A common example would be `Process` type.

```ps1
(gps | select -First 1).GetType().Name # Process
(gps | select -First 1) -is [PSObject] # True
(gps | select -First 1) -is [PSCustomObject] # True
# -is checks underlying type too
(gps | select -First 1) -is [System.Diagnostics.Process] # True
```

The functionality of ETS achieved benifits:

- Allow custom behaviours like formatting, sorting for your custom type or object.
- Dynamic interpreting a `HashTable` to any `PSObject`
- Accessibility to the underlying object.
- Manipulation on extended members.

## ETS Members


30 changes: 30 additions & 0 deletions docs/document/Powershell/docs/Object Manipulation/ForEach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ForEach

> [!TIP]
> Use `foreach` or `%` alias for `ForEach-Object`.
## Works like Select

`ForEach-Object` can do the same thing as `Select-Object -ExpandProperty`.

```ps1
gci | foreach Name
# or
gci | foreach { $_.Name }
# equivalent to
gci | select -ExpandProperty Name
```

The way `ForEach-Object` behaves is collecting implicitly returned values as an array. Every implicitly returned value will be collected as a item.

```ps1
gci | foreach { $_.Exists, $false } # True, False, True, False...
```

If you do want a `$null` return, use `Out-Null` to swallow the value.

```ps1
# doesn't make much sense though
(gci | foreach { $_.Name | Out-Null }) -eq $null # True
```

Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,19 @@
```ps1
ls | Get-Member
```

`Get-Member` returns a `MemberDefinition[]` or a `MemberDefinition` with following properties.

```cs
class MemberDefinition
{
public string Definition { get; } // expression or value or signature of the member
public System.Management.Automation.PSMemberTypes MemberType { get; } // type of member
public string Name { get; } // member name
public string TypeName { get; } // fullname of type or return type of the member
}
```

## Member Types


38 changes: 35 additions & 3 deletions docs/document/Powershell/docs/Object Manipulation/Select.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ make_vs.ps1 2.06
README.md 0.85
```

## Select Value Only
## Expand a Property

To select value of a property instead of being wrapped as an object, use `-ExpandProperty`.
### Select Value Only

To select single 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.

Expand All @@ -75,6 +77,36 @@ gps | select -ExpandProperty Name
(gps | select -ExpandProperty Name -First 1) -is [string] # True
```

> [!NOTE]
> `Foreach-Object` can achieve the same thing
```ps1
gps | foreach Name
# or use another alias of Foreach-Object %
gps | % Name
```

### Append Extra NoteProperty

`-ExpandProperty` can be used together with `-Property`, selected properties by `-Property` will be added as **NoteProperty** to the selected `-ExpandProperty`.

> [!WARNING]
> This approach mutates the selected property instead of generating a new object.
```ps1
$john = @{
Name = 'John Smith'
Age = 30
Pet = @{
Name = 'Max'
Age = 6
}
}
$john | select Name, Age -ExpandProperty Pet
```



> [!NOTE]
> `-ExpandProperty` can only take one property.
Expand All @@ -101,7 +133,7 @@ $dir = ls -Directory
$dir | select -Index 1, ($dir.Length - 1) # Pick first and last item
```

## Distinct
## Distinction

```ps1
# might have duplicated entries since file extensions should ignore casing.
Expand Down

0 comments on commit 20d18df

Please sign in to comment.