Replicating Linux’s “cd -” in PowerShell

As I continue to use PowerShell as my everyday shell, I miss a few shortcuts from Linux, most of all “cd -“, which allowed me to go back to my previous directory. So I set out to replicate this functionality in PowerShell (or at least see if someone else had done so already).

Turns out there were already a couple of implementations of this, see here and here. While these are both functional versions, I set out to create my own more compact version, using PowerShell’s pushd (Push-Location) and popd (Pop-Location) cmdlets.

The result is below, which I’ve added to my PowerShell profile, so it loads automatically every time I run PowerShell:

This is what it looks like when it works: