Skip to content

Commit

Permalink
grrr
Browse files Browse the repository at this point in the history
  • Loading branch information
sharpchen committed Nov 17, 2024
1 parent ca573e2 commit b9a35ce
Show file tree
Hide file tree
Showing 23 changed files with 292 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/document/Powershell/docs/1.Overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

- Dynamic typing
- Case insensitive
- Everything is object, more than plain text in shell.
3 changes: 3 additions & 0 deletions docs/document/Powershell/docs/Alias.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ Get-Alias | Where-Object { $_.Options -match 'ReadOnly|Constant' }
> [!note]
> Do not use custom aliases for public repository.
> [!CAUTION]
> Not all builtin aliases are available in all system. See [removed aliases](https://learn.microsoft.com/en-us/powershell/scripting/whats-new/unix-support?view=powershell-7.4#aliases-not-available-on-linux-or-macos)
## Differ from Bash

Alias in powershell is only a name alias for a command, it can never take any predetermined parameters or flags like in bash.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ Alias sl -> Set-Location

There's two ways to get current working directory in Powershell.

- `$PWD` builtin string variable. Refreshes on each time location changed.
- `$PWD` builtin `PathInfo` variable. Refreshes on each time location changed.
- `Get-Location` cmdlet. Returns a `PathInfo` object.
- `pwd` is an builtin alias for `Get-Location`.

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

```console
Expand Down
19 changes: 19 additions & 0 deletions docs/document/Powershell/docs/File System/3.Special Folders.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Special Folders

## Home Directory

- `$HOME`
- `$env:USERPROFILE`

## Temp Folder

- `$env:TMP`
- `$env:TEMP`

## APPDATA

- `$env:APPDATA`

## LOCALAPPDATA

- `$env:LOCALAPPDATA`
13 changes: 13 additions & 0 deletions docs/document/Powershell/docs/File System/5.Write to File.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Write to File



## Redirection Operator

Redirection operator in powershell is a shorthand for `Out-File`, to mimic the same thing in bash.

```ps1
ls > foo.txt
# equivalent to
ls | Out-File foo.txt
```
31 changes: 31 additions & 0 deletions docs/document/Powershell/docs/File System/6.Serialization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Serialization

> [!TIP]
> For deserialization, see `gcm -Verb Import`
## JSON

```ps1
ls | ConverTo-JSON
ls | ConverTo-JSON > foo.json
```

## XML

Powershell has a special xml format for itself called `Clixml`, this is specific to powershell.

```ps1
ls | Export-Clixml foo.xml
```


## CSV

```ps1
ls | ConverTo-Csv > foo.csv
ls | Export-Csv foo.csv
```

## HTML

## TXT
6 changes: 6 additions & 0 deletions docs/document/Powershell/docs/Language/Comment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Comment

## Single Line

## Multiple Lines

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

## String Interpolation

## Raw String

## String Concatenation

## Character Escaping
36 changes: 36 additions & 0 deletions docs/document/Powershell/docs/Modulization/Command Conflicts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Command Conflicts

Commands from modules usually ships with a prefixed name to distinguish name conflicts.
`Az` module for example, it always prefix its commands with `Az`.

```console
$ gcm Get*Az* | select -First 5

CommandType Name Version Source
----------- ---- ------- ------
Alias Get-AzADServicePrincipalCredential 7.6.0 Az.Resources
Alias Get-AzAksClusterUpgradeProfile 6.0.4 Az.Aks
Alias Get-AzApplicationGatewayAvailableSslOptions 7.10.0 Az.Network
Alias Get-AzApplicationGatewayAvailableWafRuleSets 7.10.0 Az.Network
Alias Get-AzApplicationGatewayBackendHttpSettings 7.10.0 Az.Network
```

## Command Fullname

One solution is specifying the fullname of the command.

```ps1
ModuleName\Get-Foo # use `\` as separator
```

## Add Custom Prefix

If a module doesn't have a prefix for its cmdlet, you can customize prefix for it on import.

```ps1
ipmo '<module>' -Prefix Foo
# assuming `Get-Bar` is from `<module>`
# `Get-Bar` -> `Get-FooBar`
Get-FooBar
```

63 changes: 63 additions & 0 deletions docs/document/Powershell/docs/Modulization/Load Modules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Load Modules

## Custom Module Path

Powershell automatically scans modules at certain locations.
Inspect existing module path by:

:::code-group
```ps1[Windows]
$env:PSModulePath -split ';' # windows
```
```ps1[Linux/Mac]
$env:PSModulePath -split ':' # linux/mac
```
:::

You can add your module location inside your profile file.

```ps1
$env:PSModulePath += [IO.Path]::PathSeparator + '<path>'
```

## Lazy Loading

Powershell lazily loads a module when member from it is called.

## Loaded Modules

Inspect all loaded modules in current session:

```ps1
Get-Module
```

Check whether certain module name is loaded:

```ps1
Get-Module -Name '<name>'
```

> [!TIP]
> Use `gmo` alias for `Get-Module`.
## Import Modules

Import a module in current session.

```ps1
Import-Module -Name '<name>'
```
> [!TIP]
> Use `ipmo` alias for `Import-Module`.
## Unload Modules

Unload a module from current session.

```ps1
Remove-Module '<name>'
```

> [!TIP]
> Use `rmo` alias for `Remove-Module`.
42 changes: 42 additions & 0 deletions docs/document/Powershell/docs/Modulization/Modules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Modules

## Add Module Repository

Module repository is a registry of powershell modules.
It can be public or privately hosted by yourself.

```ps1
Register-PSRepository <url>
```

## Install a Module

```ps1
Install-Module <name>
```

## Search Modules

Search module to be installed in registered repositories.

```ps1
Find-Module <pattern>
```

## Custom Module Path

Powershell automatically scans modules at certain locations.
Inspect existing module path by:

:::code-group
```ps1[Windows]
$env:PSModulePath -split ';' # windows
```
```ps1[Linux/Mac]
$env:PSModulePath -split ':' # linux/mac
```
:::

```ps1
$env:PSModulePath += [IO.Path]::PathSeparator + '<path>'
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Overview

Powershell is a Object-Oriented shell language, so inspection and manipulation on objects is the fundamental capability to work with.

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

- LINQ like transformation over objects.
- Object comparison.
- Serialization and Deserialization.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Compare Objects

> [!TIP]
> Use `diff` or `compare` alias for `Compare-Object`
`Compare-Object` is a cmdlet to compare two sources of objects and returns a indicator implying the difference between the two.

`-ReferenceObject` is the base object to compare, `-DifferenceObject` is the right hand side.

```ps1
Compare-Object -ReferenceObject @('abc', 'dfg', 4) -DifferenceObject @('fg', 'abc', 1)
```

```console
InputObject SideIndicator
----------- -------------
fg =>
1 =>
dfg <=
4 <=
```

- `<=` implies the difference content came from the `-ReferenceObject`
- `=>` implies the difference content came from the `-DifferenceObject`
- `==` implies the content appeared in both sides.

> [!NOTE]
> `Compare-Object` hides `==` case by default. Use `-IncludeEqual` to display it. And `-ExcludeDifferent` to to hide different cases.
```ps1
diff -ref @('abc', 'dfg', 4) -diff @('fg', 'abc', 1) -IncludeEqual
```

## Compare by Property

You can specify which property to compare.

```ps1
diff @('abc', 'dfg') @('fg', 'abc') -Property Length # compare on string.Length
```

## Comparison Solution

`Compare-Object` will try finding a method to do the comparison, falls back as following:

- `IComparable` interface
- `ToString()` and compare on string

> [!TIP]
> If the object to be compared doesn't implement `IComparable`, you should use `-Property`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Object Members

> [!TIP]
> Use `gm` alias for `Get-Member`.
```ps1
ls | Get-Member
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Select
Empty file.

0 comments on commit b9a35ce

Please sign in to comment.