Compare commits

...

7 commits

9 changed files with 130 additions and 40 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
Help/*
Scripts/InstalledScriptInfos/*

View file

@ -11,6 +11,17 @@ notepad "$(Split-Path -Parent $PROFILE.CurrentUserAllHosts)\my-script.ps1"
codium "$(Split-Path -Parent $PROFILE.CurrentUserAllHosts)\my-script.ps1" codium "$(Split-Path -Parent $PROFILE.CurrentUserAllHosts)\my-script.ps1"
``` ```
# Get-Help
You can learn about pretty much anything in PowerShell with the `Get-Help` cmdlet.
```powershell
Update-Help
Get-Help Get-Help
Get-Help New-Item
Get-Help Get-ChildItem
```
# variables # variables
[PowerShell is as case-insensitive as possible.](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_case-sensitivity?view=powershell-7.3) This means `$MyInvocation` works just the same as `$mYinVoCAtiOn` among other things. Coming from a Unix background and preferring some consistency I like to pretend it is case-sensitive so in this repo I'll strive for that but it's worth keeping in mind. Especially since **the Microsoft docs use case a lot** in variable and cmdlet names. [PowerShell is as case-insensitive as possible.](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_case-sensitivity?view=powershell-7.3) This means `$MyInvocation` works just the same as `$mYinVoCAtiOn` among other things. Coming from a Unix background and preferring some consistency I like to pretend it is case-sensitive so in this repo I'll strive for that but it's worth keeping in mind. Especially since **the Microsoft docs use case a lot** in variable and cmdlet names.
@ -38,6 +49,11 @@ else {
Write-Host "value is not a y" Write-Host "value is not a y"
} }
if (-not $some_switch) {
# a bang ! also works...
}
$num = 0
while ($num -lt 50) { while ($num -lt 50) {
$num = $num + 1 $num = $num + 1
} }
@ -59,10 +75,11 @@ There are [a lot of different powers](https://learn.microsoft.com/en-us/powershe
```powershell ```powershell
# Name-And-Age.ps1 # Name-And-Age.ps1
if ($silent) { exit } if (-not $silent) {
Write-Host "Hello $name" Write-Host "Hello $name"
if ($age) { if ($age) {
Write-Host "according to the -age you provided you are: $age" Write-Host "according to the -age you provided you are: $age"
}
} }
``` ```
@ -74,16 +91,6 @@ Name-And-Age.ps1 -name "Rose" -age "27"
Name-And-Age.ps1 "Rose" "27" Name-And-Age.ps1 "Rose" "27"
``` ```
# Get-Help
You can learn about the available `param` list for pretty much anything in PowerShell with the `Get-Help` cmdlet.
```powershell
Get-Help Get-Help
Get-Help New-Item
Get-Help Name-And-Age.ps1
```
# functions # functions
Functions use `param` too (instead of parenthesis like other languages). The last value is the `return` result, (which you can also use to `return` early... or just regularly if you want) Functions use `param` too (instead of parenthesis like other languages). The last value is the `return` result, (which you can also use to `return` early... or just regularly if you want)

View file

@ -10,6 +10,8 @@ Windows 11, at the time of writing, doesn't come with the latest PowerShell. It
winget install --id Microsoft.Powershell --source winget winget install --id Microsoft.Powershell --source winget
``` ```
- [ ] Install method that doesn't require git.
Then clone this repository to the desired `$PROFILE` sorta like this: Then clone this repository to the desired `$PROFILE` sorta like this:
```powershell ```powershell
@ -55,17 +57,19 @@ I also added `Install-Scoop` and `Install-BleachBit` utility functions. They are
# `$env:PATH` # `$env:PATH`
Folders in the semi-colon seperated `$env:PATH` string are searched for cmdlets, executables and scripts to run (again, if you're familiar with Bash it's similar to `$PATH`). Putting the following in `$PROFILE` will make custom `.ps1` scripts in that same folder "global" (available from any prompt without specifying the whole path). Folders in the semi-colon seperated `$env:PATH` string are searched for cmdlets, executables and scripts to run (again, if you're familiar with Bash it's similar to `$PATH`). Putting the following in `$PROFILE` will make `.ps1` files under the Scripts available globally (accessible from any terminal without specifying the whole path).
```powershell ```powershell
$env:PATH += ";$(Split-Path -Parent $MyInvocation.MyCommand.Path)" $env:PATH += ";$(Split-Path -Parent $MyInvocation.MyCommand.Path)\Scripts"
``` ```
[`Split-Path`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/split-path?view=powershell-7.3) is a pretty useful cmdlet for working with paths. Here we're saying "give me the parent folder for the current command path" which is the `$PROFILE` folder in this context. `$MyInvocation` is from the [automatic variables](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.3#myinvocation) which are provided by PowerShell under-the-hood. [`Split-Path`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/split-path?view=powershell-7.3) is a pretty useful cmdlet for working with paths. Here we're saying "give me the parent folder for the current command path" which is the `$PROFILE` folder in this context (and then we append ("\Scripts"). `$MyInvocation` is from the [automatic variables](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.3#myinvocation) which are provided by PowerShell under-the-hood.
## Update-And-Clean.ps1 ## Update-And-Clean.ps1
With this I've also added a [`Update-And-Clean.ps1`](./Update-and-Clean.ps1) script (in the `$PROFILE` folder) that will update Windows as well as `scoop` if it's installed and then run `bleachbit --preset --clean` (also only if installed) **which depends on whatever you've last checked in the BleachBit UI... so run that and set it up first!** The script will also try to re-invoke itself as Administrator if you're not in an elevated session already... which is usually. If you're familiar with Arch Linux you might say `yay`! With this I've also added a [`Update-And-Clean.ps1`](./Update-and-Clean.ps1) script (in the `$PROFILE\Scripts` folder) that will update Windows as well as `scoop` if it's installed and then run `bleachbit --preset --clean` (also only if installed) **which depends on whatever you've last checked in the BleachBit UI... so run that and set it up first!** The script will also try to re-invoke itself as Administrator if you're not in an elevated session already... which is usually. If you're familiar with Arch Linux you might say `yay`!
- [ ] As admin: Set-ExecutionPolicy RemoteSigned or Unrestricted??
Checkout the [Crash Course](./CrashCourse.md) for a terse guide on writing your own PowerShell scripts. Checkout the [Crash Course](./CrashCourse.md) for a terse guide on writing your own PowerShell scripts.

View file

@ -1,16 +1,20 @@
param( param(
[string]$url = "", [string]$url = "",
[system.io.fileinfo]$out = "~/Videos/Downloads/", [string]$out = "~/Videos/Downloads/",
[string]$namepattern = "%(title)s", [string]$namepattern = "%(title)s",
[int]$wait = 60 * 10 [int]$wait = 60 * 10
) )
if (!(Get-Command "yt-dlp.exe" -ErrorAction SilentlyContinue)) { if (-not (Get-Command "yt-dlp" -ErrorAction SilentlyContinue)) {
Write-Host "There's no yt-dlp.exe to work with!" Write-Host "There's no yt-dlp to work with!"
exit exit
} }
if (!(Test-Path $out -PathType container)) { if ($out -notmatch '\\$') {
$out += "\"
}
if (-not (Test-Path $out -PathType container)) {
New-Item $out -ItemType container New-Item $out -ItemType container
} }
@ -24,7 +28,7 @@ if ($url) {
else { else {
while ($true) { while ($true) {
$url = Read-host URL $url = Read-host URL
if (!$url) { break } if (-not $url) { break }
Start-Process yt-dlp -ArgumentList "$ytdlp_options $url" Start-Process yt-dlp -ArgumentList "$ytdlp_options $url"
} }
} }

View file

@ -5,18 +5,22 @@ param(
$scale = 128 $scale = 128
) )
if (!(Get-Command "magick.exe" -ErrorAction SilentlyContinue)) { if (-not (Get-Command "magick" -ErrorAction SilentlyContinue)) {
Write-Host "There's no magick.exe to work with!" Write-Host "There's no magick to work with!"
exit exit
} }
if ($in -notmatch '\\$') {
$in += "\"
}
if (Test-Path $in -PathType container) { if (Test-Path $in -PathType container) {
$out_dir = "$out\x$scale\" $out_dir = "$out\$(Split-Path -Leaf $in)_x$scale\"
if (!(Test-Path $out_dir)) { if (-not (Test-Path $out_dir)) {
New-Item "$out_dir" -ItemType directory New-Item "$out_dir" -ItemType directory
} }
foreach ($image in Get-ChildItem "$in*" -Include *.png) { foreach ($image in Get-ChildItem "$in*" -Include *.png, *.jpg, *.gif) {
$scaled = "$out_dir$($image.BaseName)$($image.Extension)" $scaled = "$out_dir$($image.BaseName)_x$scale$($image.Extension)"
Write-Host $scaled Write-Host $scaled
magick $image.FullName -scale "$($scale * 100)%" $scaled magick $image.FullName -scale "$($scale * 100)%" $scaled
} }

23
Scripts/Stream-Video.ps1 Normal file
View file

@ -0,0 +1,23 @@
param(
[string]$url = "",
[string]$record = "~/Videos/Streamed/{title}.ts",
[int]$retryinterval = 30,
[string]$quality = "best",
[switch]$norecord
)
if ($url) {
if (-not (Get-Command "streamlink" -ErrorAction SilentlyContinue)) {
Write-Host "There's no streamlink to work with!"
exit
}
$streamlink_opts = "--default-stream $quality"
$streamlink_opts += " --retry-streams $retryinterval"
if ($record -and -not $norecord) {
$streamlink_opts += " --record $record"
}
Write-Host "streamlink $streamlink_opts $url"
Start-Process streamlink -ArgumentList "$streamlink_opts $url"
}

23
Scripts/Stream.ps1 Normal file
View file

@ -0,0 +1,23 @@
param(
[string]$url = "",
[string]$record = "~/Videos/Streamed/{title}.ts",
[int]$retryinterval = 30,
[string]$quality = "best",
[switch]$norecord
)
if ($url) {
if (-not (Get-Command "streamlink" -ErrorAction SilentlyContinue)) {
Write-Host "There's no streamlink to work with!"
exit
}
$streamlink_opts = "--default-stream $quality"
$streamlink_opts += " --retry-streams $retryinterval"
if ($record -and -not $norecord) {
$streamlink_opts += " --record $record"
}
Write-Host "streamlink $streamlink_opts $url"
Start-Process streamlink -ArgumentList "$streamlink_opts $url"
}

View file

@ -1,13 +1,30 @@
param([switch]$elevated) param(
[switch]$elevated,
[switch]$CleanOnly
)
if (-not $CleanOnly -and -not $elevated) {
winget upgrade --all --include-unknown
}
if ((Get-Command "scoop" -ErrorAction SilentlyContinue) -and -not $elevated) {
if (-not $CleanOnly) {
scoop update
scoop update --all
scoop cleanup --al
}
scoop cache rm *
}
# If not in an Administrator environment # If not in an Administrator environment
If (!(New-Object Security.Principal.WindowsPrincipal $( If (-not (New-Object Security.Principal.WindowsPrincipal $(
[Security.Principal.WindowsIdentity]::GetCurrent() [Security.Principal.WindowsIdentity]::GetCurrent()
)).IsInRole( )).IsInRole(
[Security.Principal.WindowsBuiltinRole]::Administrator [Security.Principal.WindowsBuiltinRole]::Administrator
) )
) { ) {
# and elevating hasn't already been tried # and elevating hasn't already been tried
if (!$elevated) { if (-not $elevated) {
# attempt to become Administrator by re-invoking this script # attempt to become Administrator by re-invoking this script
Start-Process PowerShell -Verb RunAs -ArgumentList ( Start-Process PowerShell -Verb RunAs -ArgumentList (
'-file "{0}" -elevated' -f ($MyInvocation.MyCommand.Definition) '-file "{0}" -elevated' -f ($MyInvocation.MyCommand.Definition)
@ -17,13 +34,15 @@ If (!(New-Object Security.Principal.WindowsPrincipal $(
exit exit
} }
Get-WindowsUpdate -AcceptAll -Install
if (Get-Command "scoop.exe" -ErrorAction SilentlyContinue) { if (-not $CleanOnly) {
scoop update if (-not (Get-Command "Get-WindowsUpdate")) {
scoop update --all Install-Module PSWindowsUpdate
scoop cache rm * }
Get-WindowsUpdate -AcceptAll -Install
} }
if (Get-Command "bleachbit.exe" -ErrorAction SilentlyContinue) {
if (Get-Command "bleachbit" -ErrorAction SilentlyContinue) {
# Clean everything selected in the UI last time it was run. # Clean everything selected in the UI last time it was run.
# So... run The UI to set things up initially. # So... run The UI to set things up initially.
bleachbit --preset --clean bleachbit --preset --clean

View file

@ -1,5 +1,6 @@
# Scripts in this $PROFILE folder are added to the PATH # Scripts in this $PROFILE\Scripts folder are added to the PATH
$env:PATH += ";$(Split-Path -Parent $MyInvocation.MyCommand.Path)" $env:PATH += ";$(Split-Path -Parent $MyInvocation.MyCommand.Path)\Scripts"
function prompt { function prompt {
$prompt = "> " $prompt = "> "
@ -22,6 +23,9 @@ function Install-Scoop {
} }
function Install-BleachBit { function Install-BleachBit {
if (-not (Get-Command "git" -ErrorAction SilentlyContinue)) {
scoop install git
}
scoop bucket add extras scoop bucket add extras
scoop install bleachbit scoop install bleachbit
} }