create-a-page

Making PowerShell More Bash-ful

How to ease the headaches of working on windows terminals


Windows terminals suck...

At least they do if you're used to other systems. There are a few things that are quite annoying, one of the main one's being that there are 2 different scripting languages!

DOS/CMD/BAT

cmd is the older terminal that ships with windows. You can write files in command (.cmd) or batch (.bat). Generally speaking, this is the system that will give you the most low-level control in windows. Some people also refer to this as a DOS file/script. Here is an example batch file:

:: This file will go through and print some information
:: echo is how you print things, it can also read environment variables with %variable_name%

echo Hello World

:: Echo your user directory

echo %USERPOFILE%

:: Check the routing for shulichignite.com

tracert schulichignite.com

Powershell/ps1

Powershell is the new system. Files for this use .ps1 extensions and have deeper tie ins to the .net infrastructure on windows. Normally this system has some annoying quirks. For example you can no longer CD to an environment variable with the simple syntax used above for CMD/bat. We have provided a file below that is a profile you can use with powershell that will fix this issue!

# Create new global aliases
New-Alias USERPROFILE $env:userprofile
New-Alias PATH $env:path
New-Alias python3 python
New-Alias which where.exe
# NOTE: starship also adds an implicit: New-Alias ~ $env:userprofile

# Introducing a set of "aliases" (a hashmap) that will be auto-expanded to expected paths
$Custom_Alias = @{
	"userprofile"= $env:userprofile;
	"%userprofile%"= $env:userprofile;
	"programfiles"= $env:programfiles;
	"%programfiles%"= $env:programfiles;
	"appdata"= $env:appdata;
	"%appdata%"= $env:appdata;
	"desktop" = Join-Path $env:USERPROFILE 'Desktop';
	"documents" = Join-Path $env:USERPROFILE 'Documents';
	"downloads" = Join-Path $env:USERPROFILE 'Downloads';
	"development" = Join-Path $env:USERPROFILE 'development';
}

# Define custom functions to replace standard functions inside powershell

Function GoodCD($a){
    <#
    .Description
    GoodCD is a cd replacement that allows old-style path expansions such as %USERPROFILE%
    so for example `cd userprofile` will go to $env:USERPROFILE with this function
    #>

    if ($Custom_Alias.ContainsKey($a)){ 
        # Expand to correct path and navigate to it
        Set-Location -LiteralPath $Custom_Alias[$a]
    }
    else{
        Set-Location $a
    }
    
}


function GoodEcho($a){
	<#
    .Description
     GoodEcho is a echo replacement that allows old-style path expansions such as %USERPROFILE%
     #>
  
     if ($Custom_Alias.ContainsKey($a)){ 
     # Expand to correct path and navigate to it
     Write-Output $Custom_Alias[$a]
     } 
     else{
     	Write-Output $a
     }
}

# Replace standard cd with GoodCD()
Set-Alias -Name cd -Value GoodCD -Option AllScope

# Replace standard echo with GoodEcho()
Set-Alias -Name echo -Value GoodEcho -Option AllScope

To use this profile simply open up a powershell window, and then run:

 notepad $profile

From there copy-paste the file from earlier as

  %USERPOFILE%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1