documents/dev/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'
Set-Alias vi nvim

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 { 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'