56

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?

gparyani
  • 1,881
Kazark
  • 3,509

9 Answers9

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

th1rdey3
  • 465
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.

EBGreen
  • 9,655
12

I've had differing luck with cd +/- even though it should work.

pushd and popd have been around a long time and do the job nicely but I wasn't sure if they were officially supported in Powershell - I've just discovered they are actually aliases to Push-Location and Pop-Location so are current and supported, seems to be the best way to go:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/push-location?view=powershell-7 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/pop-location?view=powershell-7

Previous suggested of using aliases to swap out the standard cd command are a good idea but in scripting I'm just using the real cmdlet name for clarity.

Example of what works:

cd B:\MyFolder\MySubFolder\
Push-Location
Set-Location Env:
cd B:\
Pop-Location

returns me to B:\MyFolder\MySubFolder\

Example of what does not work:

cd B:\MyFolder\MySubFolder\
Push-Location
Set-Location Env:
Pop-Location

fails - (requires the change in drive back to the where it was Pushed)

Push-Location and Pop-Location appears to be "LIFO" (Last In First Out)

Oly Dungey
  • 416
  • 5
  • 7
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
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 :)

nitzel
  • 131
  • 2
1
  function custom_cd {
    if ($args.Count -eq 0) {
      $tmp_path = ${HOME}
    }
    elseif ($args[0] -eq '-') {
      $tmp_path = $OLDPWD;
    }
    else {
      $tmp_path = $args[0];
    }
    if ($tmp_path) {
      Set-Variable -Name OLDPWD -Value $PWD -Scope global;
      Set-Location $tmp_path;
    }
  }
  Set-Alias cd custom_cd -Option AllScope

Add this code to your $PROFILE. After that, the folowing features are available:

Command Description
cd takes you to the users home path
cd - switch to last location
cd <path> default behaviour of cd
Jagger Yu
  • 139
1

The easiest way I found is to use Push-Location and Pop-Location, especially in scripts.

Use Push-Location the same as you would use cd but it also puts your current directory on to the stack.

You can do this as many times as you want, as far as I know.

To go one level back, you just use Pop-Location

Push-Location c:\Windows\Sytem32\
ls
Pop-Location

Of course, you can make aliases for these commands as you like to shorten them for your needs.

0

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

js2010
  • 713
0

I realize the question is old... but I still hit it in 2022 because I want the same feature but not hassle about extra commands.
I ended up installing PowerShell 7.2 and modifying my shortcuts to use this more recent version.

The profile though was in a different place, but a copy command of the ps5 profile did the trick, so everything looks the same as before with the bonus of "cd -" and "cd +"

Still baffles me why in friggin heavens it was decided that "cd -" was not good enough to keep flipping back and forth, but naaaaaaahhhh... someone had to put his stinking finger and say "let's use +" to accomplish the same thing.... ok, rant is over.

AlexD
  • 283