diff --git a/README.md b/README.md index 0944e5e..749e95b 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,6 @@ No I'm not happy about this brain damage. And no there will not be "on MacOS" or 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 ``` @@ -22,45 +21,51 @@ 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! +`$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 but 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`). +## Create `$PROFILE.CurrentUserAllHosts` + +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 ``` +## Custom prompt function + A very simple first customization one can do is update the prompt. This will indicate whether or not you're running as an Administrator. ```powershell -function Test-Admin { - $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent()) - $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) -} - function prompt { $prompt = "> " - If (Test-Admin -eq $true) { + If ((New-Object Security.Principal.WindowsPrincipal $( + [Security.Principal.WindowsIdentity]::GetCurrent() + ).IsInRole( + [Security.Principal.WindowsBuiltinRole]::Administrator + ) + )) { $prompt = "Admin$prompt" } $prompt } ``` -Note the `Test-Admin` function, which is now available globally, will be used again in the `full_update.ps1` script and is generally a nice utility to have. I also added `Install-Scoop` and `Install-BleachBit` utility functions. They are totally optional but, I think, useful. +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)" +$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. +[`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. -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`! +## 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-inkoke 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`! Checkout the [Crash Course](./CrashCourse.md) for a terse guide on writing your own PowerShell scripts. @@ -68,3 +73,5 @@ Checkout the [Crash Course](./CrashCourse.md) for a terse guide on writing your - [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) +- [Scoop package manager for Windows](https://github.com/ScoopInstaller/Scoop) +- [BleachBit](https://www.bleachbit.org) diff --git a/profile.ps1 b/profile.ps1 index af3e3ad..011e6d5 100644 --- a/profile.ps1 +++ b/profile.ps1 @@ -1,14 +1,14 @@ # Scripts in this $PROFILE folder are added to the PATH -$env:path += ";$(Split-Path -Parent $MyInvocation.MyCommand.Path)" - -function Test-Admin { - $current_user = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent()) - $current_user.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) -} +$env:PATH += ";$(Split-Path -Parent $MyInvocation.MyCommand.Path)" function prompt { $prompt = "> " - If (Test-Admin -eq $true) { + If ((New-Object Security.Principal.WindowsPrincipal $( + [Security.Principal.WindowsIdentity]::GetCurrent() + ).IsInRole( + [Security.Principal.WindowsBuiltinRole]::Administrator + ) + )) { $prompt = "Admin$prompt" } $prompt diff --git a/update_and_clean.ps1 b/update_and_clean.ps1 new file mode 100644 index 0000000..798df9b --- /dev/null +++ b/update_and_clean.ps1 @@ -0,0 +1,30 @@ +param([switch]$elevated) +# If not in an Administrator environment +If (!(New-Object Security.Principal.WindowsPrincipal $( + [Security.Principal.WindowsIdentity]::GetCurrent() + ).IsInRole( + [Security.Principal.WindowsBuiltinRole]::Administrator + ) + )) { + # and elevating hasn't already been tried + if (!$elevated) { + # attmempt to become Administrator by re-invoking this script + Start-Process PowerShell -Verb RunAs -ArgumentList ( + '-noprofile -noexit -file "{0}" -elevated' -f ($MyInvocation.MyCommand.Definition) + ) + } + # otherwise just exit. + 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 +}