The Computer Oracle

Change directory to previous directory in Powershell

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Cosmic Puzzle

--

Chapters
00:00 Change Directory To Previous Directory In Powershell
00:25 Answer 1 Score 21
01:26 Answer 2 Score 3
01:44 Answer 3 Score 3
02:17 Answer 4 Score 24
02:33 Thank you

--

Full question
https://superuser.com/questions/593987/c...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#powershell #history

#avk47



ANSWER 1

Score 24


Just tried cd - on Powershell Core 6.2.2 and it works :)

cd - takes you back through your location history

cd + takes you forward through your location history




ANSWER 2

Score 21


Not in exactly the same fashion that I am aware of. One option is to use pushd instead of cd. Then popd will take you back.

You could also change your profile so that whenever a new prompt comes up (basically whenever you hit enter). It would get the PWD and compare that to the previous one. If they are different, then put that value onto a stack. Then you would include another function in your profile called something like cdb that would pop the last item off the stack and cd to it.

This sounded like fun so I came up with a solution. Put all this code into your profile (about_Profiles).

[System.Collections.Stack]$GLOBAL:dirStack = @()
$GLOBAL:oldDir = ''
$GLOBAL:addToStack = $true
function prompt
{
    Write-Host "PS $(get-location)>"  -NoNewLine -foregroundcolor Magenta
    $GLOBAL:nowPath = (Get-Location).Path
    if(($nowPath -ne $oldDir) -AND $GLOBAL:addToStack){
        $GLOBAL:dirStack.Push($oldDir)
        $GLOBAL:oldDir = $nowPath
    }
    $GLOBAL:AddToStack = $true
    return ' '
}
function BackOneDir{
    $lastDir = $GLOBAL:dirStack.Pop()
    $GLOBAL:addToStack = $false
    cd $lastDir
}
Set-Alias bd BackOneDir

Now you can cd just like normal and bd will take you back on location in your location history.




ANSWER 3

Score 3


Quick and dirty solution is to alias cd and bd to pushd and popd. A limitation is you can't do the equivalent of cd - over and over again.

Set-Alias -Name cd -Value pushd  -Option AllScope -Force
Set-Alias -Name bd -Value popd  -Option AllScope



ANSWER 4

Score 3


I've modified EBGreen's great script so that cd- will always take you to your previous directory instead of reversing through your history. This way, using cd- multiple times will toggle between two directories - which is what cd - does on unix shells.

$GLOBAL:previousDir = ''
$GLOBAL:currentDir = ''
function prompt
{
    Write-Host "PS $(get-location)>"  -NoNewLine -foregroundcolor Green
    $GLOBAL:nowPath = (Get-Location).Path
    if($nowPath -ne $GLOBAL:currentDir){
        $GLOBAL:previousDir = $GLOBAL:currentDir
        $GLOBAL:currentDir = $nowPath
    }
    return ' '
}
function BackOneDir{
    cd $GLOBAL:previousDir
}
Set-Alias cd- BackOneDir

Oh and I had to change the prompt-color to green :)