Skip to content

Instantly share code, notes, and snippets.

@travelhawk
Last active February 28, 2022 11:59
Show Gist options
  • Save travelhawk/09a6311144ff4522e59287e3d5657977 to your computer and use it in GitHub Desktop.
Save travelhawk/09a6311144ff4522e59287e3d5657977 to your computer and use it in GitHub Desktop.
Aliases for Powershell Terminal on Windows

Powershell Aliases

Add Aliases in Windows

  1. Open Windows Terminal
  2. Type notepad $profile
    • If doesn't exists, creates file in ~\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
  3. Add Aliases in file
    • Set-Alias <Name> <Command>

Useful aliases

# Creates drive shortcut for Work Folders, if current user account is using it
if (Test-Path "$env:USERPROFILE\Work Folders")
{
    New-PSDrive -Name Work -PSProvider FileSystem -Root "$env:USERPROFILE\Work Folders" -Description "Work Folders"
    function Work: { Set-Location Work: }
}

# open profile
Set-Alias -Name profile -Value "notepad $PROFILE"

Use template

The template profile file can be downloaded and used instead.

### PowerShell template profile
###
### Based on https://gist.github.com/timsneath/19867b12eee7fd5af2ba
###
### This file should be stored in $PROFILE
### PS> notepad $PROFILE
###
### As a reminder, to enable unsigned script execution of local scripts on client Windows,
### you need to run this line from an elevated PowerShell prompt:
### PS> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
# Useful shortcuts for traversing directories
function cd.. { cd .. }
function cd... { cd ..\.. }
function cd.... { cd ..\..\.. }
# Compute file hashes - useful for checking successful downloads
function md5 { Get-FileHash -Algorithm MD5 $args }
function sha1 { Get-FileHash -Algorithm SHA1 $args }
function sha256 { Get-FileHash -Algorithm SHA256 $args }
# Quick shortcut to start notepad
function n { notepad $args }
# Drive shortcuts
function HKLM: { Set-Location HKLM: }
function HKCU: { Set-Location HKCU: }
function Env: { Set-Location Env: }
# Simple function to start a new elevated process. If arguments are supplied then
# a single command is started with admin rights; if not then a new admin instance
# of PowerShell is started.
function admin
{
if ($args.Count -gt 0)
{
$argList = "& '" + $args + "'"
Start-Process "$psHome\powershell.exe" -Verb runAs -ArgumentList $argList
}
else
{
Start-Process "$psHome\powershell.exe" -Verb runAs
}
}
# Set UNIX-like aliases for the admin command, so sudo <command> will run the command
# with elevated rights.
Set-Alias -Name su -Value admin
Set-Alias -Name sudo -Value admin
# open and change powershell profile
Set-Alias -Name profile -Value "notepad $PROFILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment