I thought I’d share some of the Powershell functions and aliases I’ve found useful over the years. I’ll try to keep this post updated as I find new ones.

Powershell Aliases

Most of the aliases below are pretty self-explanatory. They are just shortcuts to common commands.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Set-Alias -Name tf -Value terraform
Set-Alias -Name k -Value kubectl
Set-Alias -Name tt -Value tree
Set-Alias -Name ll -Value ls
Set-Alias -Name g -Value git
Set-Alias -Name vim -Value nvim
Set-Alias -Name np -Value 'C:\Windows\notepad.exe'
Set-Alias -Name code -Value code-insiders
Set-Alias -Name npp -Value 'C:\Program Files\Notepad++\notepad++.exe'
Set-Alias -Name buildnumber -Value "Get-OSBuild"

Powershell Functions

This is where you can let go off your imagination and create some really cool functions. I’ll share some of the ones I’ve found useful.

Command History

I like having access to terminal history even after I close the terminal and reopen it later. So I wrote a function to do that. It uses PSReadLine plugin to query the history file and return the last 100 lines. I have this function aliased to hist.

1
2
3
4
# This function gets a history of the last 100 commands from PSReadLine log file
function hist {
    gc (Get-PSReadLineOption).HistorySavePath -tail 100
}

Open GitHub Repo in a Browser

I use this function to open the current directory in a browser directly from the command line. I have this aliased to gh.

1
2
3
4
# This function opens a GitHub repo in the browser
function gitopen {
    start (git config --get remote.origin.url)
}

Find Executable Path

I use this function to find the path of an executable. It is nifty for finding the location of things like terraform or bicep. I have this aliased to which.

1
2
3
4
5
# This function finds executables
function whereis ($command) {
    Get-Command -Name $command -ErrorAction SilentlyContinue |
    Select-Object -ExpandProperty Path -ErrorAction SilentlyContinue
}

Create A New Directory And Change To It

I use this function to create a new directory and change to it with one short alias. I have this aliased to mcd.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# This function creates a new directory and changes to it
function mcd {
    param (
        [Parameter(Mandatory = $true)]
        [string]$folderName
    )

    New-Item -ItemType Directory -Force -Path $folderName
    Set-Location -Path $folderName
}

Get OS Build Number

I am an active Windows Insider user, so I check my Windows build numbers frequently. I use this function to get the OS build number. I have this aliased to Get-OSBuild.

1
2
3
4
# This function gets the OS build number
function Get-OSBuild {
  (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name BuildLabEx).BuildLabEx.Split(".")[0]
}

Lazy Git Commit

I use this function to quickly commit changes to a git repo. I have this aliased to acp. ACP stands for Add, Commit, Push.

1
2
3
4
5
6
## Lazy Git ACP Function
function acp($commitMessage) {
    git add .
    git commit -a -m $commitMessage
    git push
}