Change directory to previous directory in Powershell

32

I am a Linux guy, but am trying to be open-minded and learn some Powershell. I miss the ability to cd - back to a previous directory, like in *nix shells. Is there a similar command in Powershell—one that would allow me to return to my previous directory?

Kazark

Posted 2013-05-10T16:09:20.020

Reputation: 2 871

5If you were to use pushd to navigate to a new directory, you could popd back to the previous one. This works in Bash, PowerShell and even the legacy Windows command line. – Bob – 2013-05-10T16:11:05.267

1

Related (though not asking about Windows, PowerShell or a direct equivalent of this command, so not an exact duplicate): How can I change to the previous directory instead of going up?

– Bob – 2013-05-11T16:55:38.640

Answers

19

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.

EBGreen

Posted 2013-05-10T16:09:20.020

Reputation: 7 834

I had the exact same inquiry as the original poster, and ended up pasting in the function definitions offered by @EBGreen. – nomadrc – 2016-05-17T12:43:48.910

11Or, you could just alias cd to pushd and bd to popd :P – Bob – 2013-05-11T10:09:28.320

1I thought about that. I just prefer making a new method to monkeying with a standard alias. Whichever is preferred is fine of course. That would be why I presented it as *a* solution rather than *the* solution. – EBGreen – 2013-05-13T13:58:06.077

5

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

th1rdey3

Posted 2013-05-10T16:09:20.020

Reputation: 225

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
Set-Alias -Name bd -Value popd  -Option AllScope

TankorSmash

Posted 2013-05-10T16:09:20.020

Reputation: 281

2

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 :)

nitzel

Posted 2013-05-10T16:09:20.020

Reputation: 121

0

You can also search through your command history with control r, and find the previous time you entered the cd command.

js2010

Posted 2013-05-10T16:09:20.020

Reputation: 321

That would work for a subset of the scenarios. As I tend to travel around with relative paths, it wouldn't work a lot of the time. – Kazark – 2019-12-06T21:18:59.203