From 85582483eda8fb7471ac86e44aa2d42f21491f25 Mon Sep 17 00:00:00 2001 From: talon Date: Sat, 17 Jun 2023 15:11:38 -0600 Subject: [PATCH] init --- CrashCourse.md | 24 +++++++++++++++++ README.md | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ full_update.ps1 | 30 +++++++++++++++++++++ profile.ps1 | 29 ++++++++++++++++++++ 4 files changed, 155 insertions(+) create mode 100644 CrashCourse.md create mode 100644 README.md create mode 100644 full_update.ps1 create mode 100644 profile.ps1 diff --git a/CrashCourse.md b/CrashCourse.md new file mode 100644 index 0000000..944d9f7 --- /dev/null +++ b/CrashCourse.md @@ -0,0 +1,24 @@ +# PowerShell Scripting Crash Course + +There are a few different ways to get started. + +```powershell +# Open the $PROFILE folder +explorer (Split-Path -Parent $PROFILE.CurrentUserAllHosts) +codium (Split-Path -Parent $PROFILE.CurrentUserAllHosts) +# Or start directly editing a script +notepad "$(Split-Path -Parent $PROFILE.CurrentUserAllHosts)\my-script.ps1" +codium "$(Split-Path -Parent $PROFILE.CurrentUserAllHosts)\my-script.ps1" +``` + +# param + +# Read-Host + +# Paths and Items + +# If, For and While + +# See Also + +- [About scripts (learn.microsoft.com)](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scripts?view=powershell-7.3) diff --git a/README.md b/README.md new file mode 100644 index 0000000..97cd40d --- /dev/null +++ b/README.md @@ -0,0 +1,72 @@ +# PowerShell on Windows + +No I'm not happy about this brain damage. And no there will not be "on MacOS" or "on Linux" flavors, from me anyway. + +# Install and setup + +Windows 11, at the time of writing, doesn't come with the latest PowerShell. It will tell you that when you open PowerShell even. You can acquire it like this (or [some other way](https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.3)): + +```powershell +winget search Microsoft.PowerShell +winget install --id Microsoft.Powershell --source winget +``` + +Then clone this repository to the desired `$PROFILE` sorta like this: + +```powershell +# get the URL from wherever this repo is hosted +git clone (Split-Path -Parent $PROFILE.CurrentUserAllHosts) +``` + +Or follow along below to understand what this repository is about! + +# `$PROFILE` + +`$PROFILE.CurrentUserAllHosts` is a good place to make customizations (if you're familiar with Bash it's similar to `~/.bashrc`). There is a specific `$PROFILE.CurrentUserCurrentHost` when you're inside VSCodium and other Hosts if you're interested in that, they all inherit from `$PROFILE.CurrentUserAllHosts` which is probably `~/Documents/PowerShell/profile.ps1` by the way! + +You can create a profile with `New-Item` and start editing it with `notepad` (or `codium`). + +```powershell +New-Item -Path $PROFILE.CurrentUserAllHosts -ItemType "file" -Force +notepad $PROFILE.CurrentUserAllHosts +``` + +A very simple first customization one can do is update the prompt. This will indicate whether or not you're running as an Adminsitrator. + +```powershell +function prompt { + $prompt = "> " + $current_user = New-Object Security.Principal.WindowsPrincipal( + [Security.Principal.WindowsIdentity]::GetCurrent() + ) + + If ($current_user.IsInRole( + [Security.Principal.WindowsBuiltinRole]::Administrator + )) { + $prompt = "Admin$prompt" + } + + $prompt +} +``` + +I also added `install_scoop` and `install_bleachbit` utility functions. They are totally optional but, I think, useful. + +# `$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). + +```powershell +$env:path += ";$(Split-Path -Parent $MyInvocation.MyCommand.Path)" +``` + +[`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", the current command is `$PROFILE` 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. + +With this I've also added a `full_update.ps1` script 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 run itself again 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`! + +Checkout the [Crash Course](./CrashCourse.md) for a terse guide on writing your own PowerShell scripts. + +# See Also + +- [About profile (learn.microsoft.com)](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7.3) +- [Creating profiles (learn.microsoft.com)](https://learn.microsoft.com/en-us/powershell/scripting/learn/shell/creating-profiles?view=powershell-7.3) diff --git a/full_update.ps1 b/full_update.ps1 new file mode 100644 index 0000000..eb002f1 --- /dev/null +++ b/full_update.ps1 @@ -0,0 +1,30 @@ +param([switch]$Elevated) + +function Test-Admin { + $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent()) + $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) +} + +if ((Test-Admin) -eq $false) { + if ($elevated) { + # tried to elevate, did not work, aborting + } + else { + Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition)) + } + exit +} + +Get-WindowsUpdate -AcceptAll -Install + +if (Get-Command "scoop.exe" -ErrorAction SilentlyContinue) { + scoop update + scoop update --all + scoop cache rm * +} + +if (Get-Command "bleachbit.exe" -ErrorAction SilentlyContinue) { + # Clean everything selected in the UI last time it was run. + # So... run The UI to set things up initially. + bleachbit --preset --clean +} diff --git a/profile.ps1 b/profile.ps1 new file mode 100644 index 0000000..36df8d1 --- /dev/null +++ b/profile.ps1 @@ -0,0 +1,29 @@ +# Scripts in this $PROFILE folder are added to the PATH +$env:path += ";$(Split-Path -Parent $MyInvocation.MyCommand.Path)" + +function prompt { + $prompt = "> " + $current_user = New-Object Security.Principal.WindowsPrincipal( + [Security.Principal.WindowsIdentity]::GetCurrent() + ) + + If ($current_user.IsInRole( + [Security.Principal.WindowsBuiltinRole]::Administrator + )) { + $prompt = "Admin$prompt" + } + + $prompt +} + +function install_scoop { + # You can read the script before executing it here: + # https://raw.githubusercontent.com/scoopinstaller/install/master/install.ps1 + Invoke-RestMethod get.scoop.sh | Invoke-Expression + # (un)install docs: https://github.com/ScoopInstaller/Install +} + +function install_bleachbit { + scoop bucket add extras + scoop install bleachbit +}