Introduction
A command that many shells have had forever and that PowerShell has had for a long time as well is ‘cd -’ which means “change directory to previous folder”. This means you could do something like this:
|
|
This is quite handy in many situations. In particular, since I’ve modified my PowerShell Profile to NOT start in my users folder but instead to start in my c:\dev folder (from which I can easily get to any repo I may be working from), I very often find myself using cd - whenever I create a new terminal in the folder I want to be in such as when using VS Code.
The default behavior in VS Code when you say ‘New Terminal’ is to create it in the root of the folder that VS Code is running in. However in my case it always ends up being ‘c:\dev’ because of the aforementioned $PROFILE. This is easy enough for me to fix with… cd -. Works great. Less filling. That is until…
Upgrading to PowerShell 7 Breaks cd - Behavior
I recently upgraded to PowerShell 7 using something like this:
|
|
Once I did, I ran into issues with my oh-my-posh script. I tried upgrading oh-my-posh:
|
|
But that didn’t fix the issue. I was getting errors like these when running . $PROFILE:
|
|
It turns out there were a few problems. One I needed to change the instances of Get-PSReadLineKeyHandler Spacebar (and similar) to add -Chord and two I needed to upgrade my Get-PSReadLineKeyHandler because apparently it is NOT upgraded when you upgrade PowerShell.
So you check your version with this:
|
|
You want a version like 2.4.5 not 2.0.0.
To install a new version you use:
|
|
Then after restarting PowerShell you should be able to run your profile without errors:
|
|
But none of that is related to the cd - breakage - that’s just new fun stuff you may run into as you upgrade PowerShell.
Fix CD - in PowerShell 7
To fix the cd - behavior you just need to add a custom function to your $PROFILE and make sure it’s being used, not the default alias. Also, use it to set your default starting folder if that’s a thing you do:
|
|
With this, I can now open up a terminal in VS Code and then type cd - to get back to the folder I want to be in.
Why Not Just Not Change Folders?
An even better solution, for those of you wondering, would be to not change folders when PowerShell is starting in some working folder where I actually want to be (like when VS Code or some AI agent does it), but to only use my preferred default location when PowerShell is being launched without a starting folder (and thus would drop me in my users folder).
To do that, all that’s needed is a small tweak to test to see where I am when PowerShell is starting, and only change if I happen to be in my users home folder:
|
|
With this change, when I open a new terminal in VS Code, it stays in the proper folder, and when I create a new PowerShell window or tab, it starts in my default c:\dev folder. And either way, I can use cd - to navigate back from any folder I change to.
Side note, I also came across zoxide which is basically a super-powered cd replacement that works in all major shells. You can install it on windows with winget install ajeetdsouza.zoxide. I haven’t tried using it, yet, but it looks interesting for sure.
My Full $PROFILE
For anyone who cares and mostly for me on other/future machines, here’s my current full $PROFILE with these updates:
|
|
Conclusion
PowerShell 7 broke the built-in cd - functionality, but it’s easy to restore with a custom function in your profile. The solution not only brings back this convenient navigation shortcut but also gives you fine-grained control over when PowerShell changes your starting directory. Whether you’re navigating between project folders or working with VS Code’s integrated terminal, these tweaks will make your PowerShell experience more efficient.