Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
If 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 May 10 '13 at 16:11
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 May 11 '13 at 16:55
up vote 13 down vote accepted

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

share|improve this answer
Or, you could just alias cd to pushd and bd to popd :P – Bob May 11 '13 at 10:09
I 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 May 13 '13 at 13:58
I had the exact same inquiry as the original poster, and ended up pasting in the function definitions offered by @EBGreen. – nomad.rc May 17 at 12:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.