cd c:\windows\system32\opensh
cd c:\windows\
cd - (First time it does right. It switches directory to c:\windows\system32\openssh)
cd - (From now on.. it's broken. It switches to c:\)
Just like Unix, cd - should only switch between last 2 directories.
In this case, second command "cd -" should switch back to c:\windows
Second command "cd -" switch back to "c:\".
Subsequent "cd -" commands gives error message.
PS C:\> $psversiontable
Name Value
---- -----
PSVersion 6.2.3
PSEdition Core
GitCommitId 6.2.3
OS Microsoft Windows 10.0.19013
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
PS C:\>
Windows

Unix

I could be misremembering, but I think the documented behaviour for cd - in PowerShell is to go back to the previous directory... and further usages will actually go back through the history even further. I think you need to do cd + to step forward through the location stack.
I think @rkeithhill was one of the folx that helped implement this feature, if I'm not totally out of it today; perhaps he can explain a little more clearly. 馃槉
Why do we want to deviate from the UNIX behavior?
It helps UNIX users to easily adapt to Powershell if we maintain consistency with UNIX.
We can always add new features but we shouldn't deviate implementation between Windows and UNIX.
I didn't implement this for PowerShell Core (but I did for the PSCX module). But @vexx32 is correct that the PS impl of CD maintains a stack that you navigate backward and forward using - and +.
Ah, sorry, @rkeithhill. 馃榿
I couldn't recall who it was. Looks like it was https://github.com/PowerShell/PowerShell/pull/7206 by @bergmeister 馃檪
Yes, I did the feature of cd - and cd + as there was nothing before.
The standard behaviour of bash is too simplistic as it was from the 80s when there was not much memory available and just works by storing the previous location in an environment variable.
When I proposed the first draft of the implementation, I actually implemented exactly this and the PS team did not like it and the community encouraged me to implement a history stack similar to how the fish shell (a Unix shell) does cd + and cd -, hence why the current implementation can go back up to 25 location (I had to negotiate hard on getting that number high). Although it was more work, I love being able to back and forth in history more than just once, especially when I switch between different git repos.
You can follow the history and PR discussion in the 2 PRs #5051 and #7206 and related issue #2188
The best you could maybe do is to propose a setting but the PowerShell team generally does not like this idea very much due to the test coverage becoming complicated. I want to stress this a shell feature and nothing baked into Unix, so we make it only hard for Unix people using shells from the 80s and not modern shells. I know bash is still ubiquitous so take that statement with a pinch of salt 馃構
Otherwise this is by design 馃槑
As a UNIX user, I didn't like the implementation. I would like to be inline with UNIX implementation.
I am sure UNIX users (who are new to powershell) simply try "cd -" assuming it's supported and they don't know "cd +".. Even if you tell them, they don't like it.
I would suggest to change this implementation.. You can still achieve the same feature of maintaining the history by adding something like "cd - < number >" (example, cd - 2) which will look at the stack and goes back to the desired directory.
I've said it before, there is no such thing as an UNIX implementation. It is a shell implementation. Common Unix shells like fish have the exact same behaviour as PowerShell where cd - has a location history of the last 25 locations:
https://fishshell.com/docs/2.4/commands.html
Old shells like bash cannot improve their behaviour for legacy reasons. Therefore this is purely about educating users and the onus will always be on user's side to stay up to date, which is the nature of the IT industry (which is good as it as a way of moving forward)
Most helpful comment
Yes, I did the feature of cd - and cd + as there was nothing before.
The standard behaviour of bash is too simplistic as it was from the 80s when there was not much memory available and just works by storing the previous location in an environment variable.
When I proposed the first draft of the implementation, I actually implemented exactly this and the PS team did not like it and the community encouraged me to implement a history stack similar to how the fish shell (a Unix shell) does cd + and cd -, hence why the current implementation can go back up to 25 location (I had to negotiate hard on getting that number high). Although it was more work, I love being able to back and forth in history more than just once, especially when I switch between different git repos.
You can follow the history and PR discussion in the 2 PRs #5051 and #7206 and related issue #2188
The best you could maybe do is to propose a setting but the PowerShell team generally does not like this idea very much due to the test coverage becoming complicated. I want to stress this a shell feature and nothing baked into Unix, so we make it only hard for Unix people using shells from the 80s and not modern shells. I know bash is still ubiquitous so take that statement with a pinch of salt 馃構
Otherwise this is by design 馃槑