A quick $PROFILE setup guide, a crash course on writing scripts and some scripts I wrote.
Go to file
2023-06-23 20:50:33 -06:00
Scripts WIP 2023-06-23 20:50:33 -06:00
CrashCourse.md idiomatic-ish updates 2023-06-22 09:45:19 -06:00
profile.ps1 WIP 2023-06-23 20:50:33 -06:00
README.md WIP 2023-06-23 20:50:33 -06:00

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):

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:

# get the URL from wherever this repo is hosted
git clone <url> (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 but they all inherit from $PROFILE.CurrentUserAllHosts which is probably ~/Documents/PowerShell/profile.ps1 by the way!

Create $PROFILE.CurrentUserAllHosts

Create a profile with New-Item and start editing it with notepad (or codium).

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.

function prompt {
  $prompt = "> "
  if ((New-Object Security.Principal.WindowsPrincipal $(
        [Security.Principal.WindowsIdentity]::GetCurrent()
      )).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 .ps1 files under the Scripts available globally (accessible from any terminal without specifying the whole path).

$env:PATH += ";$(Split-Path -Parent $MyInvocation.MyCommand.Path)\Scripts"

Split-Path 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 which are provided by PowerShell under-the-hood.

Update-And-Clean.ps1

With this I've also added a 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 for a terse guide on writing your own PowerShell scripts.

See Also