39

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?

  • 5
    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
  • 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 May 11 '13 at 16:55
1

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.

| improve this answer | |
  • 1
    AFAIK this is now the correct answer---i.e. it looks from the link there like in the 7+ years since I asked, they have been added to the language. – Kazark Jul 2 at 20:45
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.

| improve this answer | |
  • 12
    Or, you could just alias cd to pushd and bd to popd :P – Bob May 11 '13 at 10:09
  • 1
    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. – nomadrc May 17 '16 at 12:43
12

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

| improve this answer | |
  • 2
    help cd ... PowerShell 6.2 added support for '-' and '+' with the Path parameter. PowerShell maintains a history of the last 20 locations that can be [sic]access with - and +. This list is independent from the location stack that is accessed using the StackName parameter. Good catch! – Nathan Chappell Mar 19 at 20:36
  • We have PS 7 now so I found this the most natural answer in 2020 – guillem Aug 11 at 10:44
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
| improve this answer | |
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 :)

| improve this answer | |
0

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

| improve this answer | |
  • 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 Dec 6 '19 at 21:18
0
  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
| improve this answer | |
  • Explaining what this does and how to use will make it an answer. Right now it's just a bit of code without context. – music2myear Sep 15 at 2:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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