Install-Software "pwsh-sketches"
Install the "pwsh-sketches", my PowerShell scripts via GitHub.
Introduction
Install my personal script set: “pwsh-sketches ,” which makes daily shell operations slightly more convenient and brings petit ideas to life. The script set is hosted on GitHub and can be installed on your PC using the version control system Git .
Key point: One of the interesting things about the shell is that you can easily craft useful tools tailored to your own work and level.
Disclaimer
“pwsh-sketches ” is an experimental set of PowerShell scripts for prototyping and exploring ideas. Note that while those scripts have been tested by the author to work, they are not intended for production use and may be unstable or incomplete.
License (Copyright)
pwsh-sketches
is released on GitHub
under the MIT License
. You are free to modify and use it under this license. If you publish a modified script, Copyright notices (e.g. Copyright <Year of First Release> <Copyright Holder's Name>
) should be included at the beginning of the source code or in the project’s Readme.md
as follows:
pwsh-sketches: Copyright 2025 btklab under The MIT License
- The MIT License: https://opensource.org/license/mit
- btklab/pwsh-sketches: https://github.com/btklab/pwsh-sketches
Prerequisites
- ✓ Install PowerShell
- ✓ Install Git
- ✓ (Optional) Create a working directory
- ✓ (Optional) Learn PowerShell Basic
- ✓ Verified environment can be found here
Cloning the Repository
- Launch PowerShell
- Create and Move into the directory where you will place the repository
- Example: Move into the
bin
directory created in Create a working directorymkdir ~/cms/bin
pushd ~/cms/bin
- Example: Move into the
- Clone the scripts from GitHub
- Example:
git clone https://github.com/btklab/pwsh-sketches.git
- Example:
- Installation completed
↓ Directory structure after installation (example)
home
└─cms
├─bin
│ └─pwsh-sketches <-- Repository cloned from GitHub
│
└─drafts
Installation: Minimum
Key point: With minimal installation, you can load only the functions you want (via dot-source).
Move into the cloned repository and ensure that the files and folders are in place.
First, Move into the cloned repository using the cd
or pushd
command:
# Move into the cloned repository
pushd ~/cms/bin/pwsh-sketches
By the way, you can quickly and accurately enter the path using TAB completion. If there are multiple candidates, pressing TAB, TAB, … will display the candidates sequentially:
- Enter
~/cm
<TAB>, it completes to~/cms/
- Enter
~/cms/bi
<TAB>, it completes to~/cms/bin/
- Enter
~/cms/bin/pw
<TAB>, and the rest is completed
Next, use the ls
command to see if the repository files and folders have been cloned correctly:
## at ~/cms/bin/pwsh-sketches
ls
Output:
Directory: C:/Users/btklab/cms/bin/pwsh-sketches
Mode LastWriteTime Length Name
---- ------------- ------ ----
d-r-- 2024/04/27 10:00 .github
d-r-- 2024/10/31 14:43 examples
d-r-- 2024/04/27 10:00 img
d-r-- 2025/03/02 22:31 src
d-r-- 2024/04/27 10:00 tests
-a--- 2023/10/18 6:57 107 .gitignore
-a--- 2025/03/06 1:01 7595 CHANGELOG.md
-a--- 2024/08/06 16:54 106 examples.md
-a--- 2023/01/04 11:50 1084 LICENSE
-a--- 2023/12/05 7:39 2357 operator-extra.ps1
-a--- 2025/02/15 10:33 378 operator-minimum.ps1
-a--- 2025/03/02 22:49 7580 operator.ps1
-a--- 2025/03/02 22:50 387678 README.md
Return to the original directory
popd
Let’s try executing a command as a test. Before that, some points to note:
pwsh-sketches
is basically a collection of one function per file.- It is not a PowerShell module.
- Some functions have dependencies on other functions.
- Loading
pwsh-sketches
is done by directly specifying the script file using dot-source.- For example,
. operator.ps1
. - The
Import-Module
command is not used.
- For example,
- Loaded functions are initialized when the window is closed.
- Functions need to be reloaded every time PowerShell is started.
- If this process is troublesome, you can add the script to your profile for automatic loading.
First, let’s change the character encoding to UTF-8:
- This operation is probably unnecessary for PowerShell 6 and later (the default value is already UTF-8).
- I use PowerShell as a glue language with other programming languages, so I explicitly specify and unify the character encoding used in all languages.
- However, if you are using Windows PowerShell (PowerShell 5), this operation is necessary.
# Move to the repository
pushd ~/cms/bin/pwsh-sketches
# Dot-source the script to change the character encoding to UTF-8
. ./operator-minimum.ps1
Here is the content of operator-minimum.ps1
:
# for windows only
if ($IsWindows){
## set encode to utf8
chcp 65001
#$OutputEncoding = [System.Text.Encoding]::UTF8
[System.Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding("utf-8")
[System.Console]::InputEncoding = [System.Text.Encoding]::GetEncoding("utf-8")
# compartible with multi byte code
$env:LESSCHARSET = "utf-8"
}
Next, load the function file to be used with dot-source:
- “Load with dot-source” refers to the method of specifying the file name after a dot (
.
) and a space. - This makes the contents of the function file available only in the current session (current scope).
- “Available only in the current session” means that it can be used until you exit the current session (i.e., exit with
exit
, start another new session, etc.). - It is a temporary, not permanent, loading.
- This method can be considered cumbersome if you think you have to load the necessary functions with dot-source every time you start PowerShell.
- On the other hand, if you think that the loading is initialized when you close the window, you can control the state where unnecessary functions accumulate and slow down the operation.
Let’s try it. Here is an example of Dot-Source the grep command:
## Case.1 Dot-Source from the root repository of pwsh-sketches
## Note the dot "." and space before the filename!
## at ~/cms/bin/pwsh-sketches
. ./src/grep_function.ps1
## Case.2 Dot-Source from any other location
## Note the dot "." and space before the filename!
## at ~
. ~/cms/bin/pwsh-sketches/src/grep_function.ps1
## Case.3 Dot-Source from the Working Directory "drafts"
## Specify with a Relative Path
## at ~/cms/drafts
. ../bin/pwsh-sketches/src/grep_function.ps1
Note: The relative positions of the working directories drafts
and pwsh-sketches
assumed in “Case.3” are shown below. As an aside, ../
in a relative path means “the directory one level above the current location” (the parent directory). And ../../
means “the directory two levels above the current location.”
home
└─cms
├─bin
│ └─pwsh-sketches <-- Repository cloned from GitHub
│ └─src
│ └─grep_function.ps1
│
└─drafts <-- Working directory
└─here_you_are
The grep
command is a filter command that returns only lines that match a specified pattern ([regular expression][]) for each line of input text (text object) that comes through the pipeline. Simply put, grep
is a “pattern-match searcher for the input text.”
Let’s use the grep
command. First, create the input for the grep
command. Enumerate numbers from 22
to 26
(reference: Microsoft.Learn – Range Operator ..
from about_Operators
):
22..26
Output:
22
23
24
25
26
Pass this output to the grep
command using the pipeline (|
):
22..26 | grep '3'
Output. Line numbers and single spaces are added to the beginning of each line:
23
Key point: The
grep
function inpwsh-sketches
is a wrapper command for the PowerShell cmdlet “Select-String ”. The behavior of its options is based on the Linux “grep ” command.
You can get help for the grep
command as follows:
Get-Help grep
You can also use the alias man
for the Get-Help
command:
man grep
Installation: Bulk
Key point: Bulk installation loads all functions at once, eliminating the need to be aware of individual function names, but it takes longer to load than minimal installation.
Function files in pwsh-sketches
can be loaded all at once. The following is an example of loading pwsh-sketches
function files all at once using operator.ps1
in the repository root. Again, the loaded functions are initialized by closing the current session (i.e. closing a window, executing an exit
command, starting a new session, etc.).
## Case.1 Running from the Root Directory of pwsh-sketches
## Note: There is a dot (.) and a space before the file name!
## at ~/cms/bin/pwsh-sketches
. ./operator.ps1
## Case.2 Running from Another Directory
## Note: There is a dot (.) and a space before the file name!
## at ~
. ~/cms/bin/pwsh-sketches/operator.ps1
## Case.3 Running from the Working Directory "drafts"
## Specify with a Relative Path
## at ~/cms/drafts
. ../bin/pwsh-sketches/operator.ps1
Note: As written before, the location relationship between the working directory drafts
and pwsh-sketches
assumed in “Case.3” is shown below. Also, as written before, ../
in a relative path means “the directory one level up from the current location” (parent directory), and ../../
means “the directory two levels up from the current location”.
home
└─cms
├─bin
│ └─pwsh-sketches <-- Repository cloned from GitHub
│ └─operator.ps1
│
└─drafts <-- Working directory
└─here_you_are
Note: Some functions in pwsh-sketches
have aliases. When you load operator.ps1
, the aliases are also loaded simultaneously. If an alias with the same name already exists, an error message will be displayed during the loading of operator.ps1
. In that case, you need to either rename the alias in the pwsh-sketches
function file or delete the existing alias.
Verify the installation of pwsh-sketches
. Try running the command man2
, which lists the functions loaded by pwsh-sketches
:
man2
Output:
Add-ForEach Get-Last Unique-Object getfirst pwsync
Add-ID Get-OGP Unzip-Archive getlast retu
Add-LineBreakEndOfFile Get-Ticket addb grep rev2
Add-LineBreak GetValueFrom-Key addl gyo rev
Add-Quartile Grep-Block addr han say
Add-Stats GroupBy-Object addt head sed-i
Apply-Function Inkscape-Converter cat2 image2md sed
Auto-Clip Invoke-Link catcsv jl self
Cast-Date Join-Line2 chead json2txt seq2pu
Cast-Decimal Join2-Object clip2file juni sleepy
Cast-Double Measure-Quartile clip2hyperlink keta sm2
Cast-Integer Measure-Stats clip2img kinsoku summary
ClipImageFrom-File Measure-Summary clip2normalize lcalc2 table2md
ConvImage Override-Yaml clip2push lcalc tac
Decode-Uri Plot-BarChart clip2shortcut linkcheck tail-f
Decrease-Indent PullOut-String conv linkextract tail
Delete-Field Rename-Normalize count list2table tarr
Detect-XrsAnomaly Replace-ForEach csv2sqlite logi2dot tateyoko
Drop-NA Replace-NA csv2txt logi2pu teatimer
Edit-Function Select-Field ctail2 man2 tenki
Edit-Property Set-DotEnv ctail map2 tex2pdf
Encode-Uri Set-Lang decil math2tex toml2psobject
Execute-Lang Set-NowTime2Clipboard delf mdfocus uniq
Execute-RMarkdown Shorten-PropertyName dot2gviz mdgrep vbStrConv
Execute-TinyTex Shutdown-ComputerAFM filehame mind2dot watercss
ForEach-Block Sleep-ComputerAFM fillretu mind2pu wrap
ForEach-Step Sleep-Computer flat movw yarr
Get-AppShortcut Sort-Block flow2pu pawk ycalc
Get-ClipboardAlternative Tee-Clip fpath percentile ysort
Get-DateAlternative Test-isAsciiLine fval pu2java zen
Get-First Transpose-Property fwatch push2loc
Get-Histogram Trim-EmptyLine gantt2pu pwmake
Terms
- pwsh-sketches
-
My script set to make everyday shell operations a little more convenient and to shape a few ideas. It is publicly available on GitHub . Please note that the source code in this repository should be viewed as an idea for command implementation, not as a source code example. Please keep in mind that this script set was implemented as quickly as possible as a prototype for my own use by embodying my ideas, and the code is difficult to maintain and contains many bugs.
-
“My” in Japanese means “個人的/私的” (こじんてき・してき: ko-jin-teki/shi-teki). In this article, “My” is used in the sense of “自家製” (じかせい: jika-sei) or “自家醸造” (じかじょうぞう: jika-jou-zou), meaning “homebrewed”. So,
pwsh-sketches
means “a script set I made myself for private use.” - grep – pwsh-sketches
-
A filter command (function) for string search included in pwsh-sketches .
-
Returns only the lines that contain the specified string pattern for each line of input (text object) that has passed through the pipeline.
-
The implementation is a wrapper script for Select-String .
-
Note that the specified string pattern is a regular expression , not a simple string. Some symbols, such as
.
,*
,+
,?
,^
,$
,[]
,{}
,()
,|
,\
, etc., have special meanings as metacharacters in regular expression. -
If you want to search for a simple string instead of a regular expression, specify the
-SimpleMatch
parameter.