hardware/windows/Windows powershell.md

PowerShell

Personal notes & cheatsheet for PowerShell on Windows.


Setup

Enable Script Execution

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

Create a Shortcut

Launch a script with bypass policy:

powershell.exe -noexit -ExecutionPolicy Bypass -File c:\data\winaero\winaero.ps1

Profiles

Profile Locations (PowerShell 5)

Scope Path
AllUsersAllHosts C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts C:\Users\domin\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost C:\Users\domin\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Profile Locations (PowerShell 7)

Scope Path
AllUsersAllHosts C:\Program Files\PowerShell\7\profile.ps1
AllUsersCurrentHost C:\Program Files\PowerShell\7\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts C:\Users\domin\Documents\PowerShell\profile.ps1
CurrentUserCurrentHost C:\Users\domin\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

Source the old PS5 profile from PS7:

. C:\Users\domin\Documents\WindowsPowerShell\profile.ps1

List All Profile Paths

foreach ($profileLocation in ($PROFILE | Get-Member -MemberType NoteProperty).Name) {
    Write-Host "$($profileLocation): $($PROFILE.$profileLocation)"
}

Profile Contents

Current Microsoft.PowerShell_profile.ps1:

#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
If (Test-Path "E:\Work\miniconda3\Scripts\conda.exe") {
    (& "E:\Work\miniconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
}
#endregion

# Aliases
Set-Alias subl 'C:\Program Files\Sublime Text 3\subl.exe'
function vi { & nvim @args }

Why a function instead of Set-Alias vi nvim: Windows PowerShell 5.1 has a quoting bug where an alias to a native command invoked with zero arguments appends a spurious empty argument, producing a malformed quoted command line. That broke scoop's shim for nvim (Shim: Could not create process with command '"...\nvim.exe" '). function vi { & nvim @args } always builds a clean argument array and works on both PS 5.1 and PowerShell 7. See ssh.md for a second, unrelated cause of the same symptom over SSH.


Z (Fast Directory Jumping)

# https://www.powershellgallery.com/packages/z/1.1.14
Install-Module -Name z -RequiredVersion 1.1.14

Theme & Fonts

Fonts

PSReadLine & posh-git

Run in order:

Update-Module PowerShellGet -Force
powershell -noprofile -command "Install-Module PSReadLine -Force -SkipPublisherCheck -AllowPrerelease"
Install-Module -Name posh-git -AllowPrerelease -Force

Windows Terminal: Panes

Docs: https://learn.microsoft.com/en-us/windows/terminal/panes

Shortcut Action
Alt+Shift++ Vertical split
Alt+Shift+- Horizontal split

Custom Functions

Sourcing Functions from Files

In your profile, add:

Get-ChildItem E:\Apps\PowershellFunctions\*Function.ps1 | % { . $_ }
function open([string]$arg1) { Invoke-Item "$arg1" }
function work { cd E:\Work }
function notes { cd E:\Work\notes }
function notes-code { code E:\Work\notes }

Listing

function ll { ls -Force }

Git Shortcuts

function b          { git branch }
function s          { git status }
function gd         { git diff }
function push       { git push }
function gco([string]$arg1) { git checkout "$arg1" }
function gca([string]$arg1) { git commit -am "$arg1" }
function inc        { git commit -am inc; git push; }
function current-branch { git rev-parse --abbrev-ref HEAD }
function p {
    $branch = current-branch
    git pull origin $branch
}

Environment Variables

Get a Variable

$Env:<variable-name>
$Env:windir   # C:\Windows

Set a Variable

$Env:<variable-name> = "<new-value>"
$Env:Foo = 'An example'