16 Jul

Slightly modified “CD” Command for Powershell

Category:General PostTag: :


In my previous job, I spent all my development time in a Linux environment. Was rather impressed at how much could get done at the command line, and how efficient some of those tasks became. My next job was based on Windows and mostly the Microsoft stack of development tools. This  meant I pretty much left the command line behind. That was, until, I started using git. And since I wanted to learn PowerShell, I used PowerShell to execute my git commands.

One thing that has bugged me for a while is simply moving between directories. Even with tab completion, all that typing is a still quite annoying. Especially if you jump between a set of similar directories. One feature from the Linux CD command that I missed was ?CD -". This command in Linux can be used to jump to the previous directory (and then back again). One limitation of this command is it only could jump back to the previous directory, and it did not retain a memory of recent directories. There may be something better in Linux that I don?t know of, but I?m basing this on a limited experience a number of years ago.

So I threw a question out on twitter.
After several tweets back and forth, @cwprogram threw an interesting spike at me.

image_thumb9[4]  http://pastebin.com/xwtkn0am
Although this wasn?t exactly what I was looking for, it contained enough of what I needed to spark my curiosity to write a version of my own.

And so a little script was born that I?m now using to replace the ?CD? command in my PowerShell runtime.

What does this do?

After you get it installed (see install steps below), when you type ?CD? with no parameters at the command prompt. It will list up to 10 of the most recent distinct paths you?ve been to recently. This list also gives an index lookup number that you can use as a shortcut to jump to that path.


C:\code> cd
     1) C:\Users\jasonj
     2) D:\temp
C:\code> cd 2

You can continue to use the ?CD? command to do your usual changing directories. Now you can quickly get a history of where you?ve been, and quickly jump to any of those previous histories without typing the entire paths again.

It defaults to only showing you the last 10 distinct items, but if you find yourself needing to go back farther than that, you can use the following command to list more than 10 items.

D:\temp> cd -ShowCount 100

How to Install

  1. Download the file and save it to a location you can reference later.
  2. Open your $PROFILE (What is that?)
  3. Type the following two commands into your profile to replace the existing ?CD? command with the new one.

    Remove-Item alias:cd
    Set-Alias cd {Your_Saved_Directory}\Change-Directory.ps1

  4. Exit your PowerShell console and start a new one up.


Happy Commanding!

4 thoughts on “Slightly modified “CD” Command for Powershell

  1. Check out the pushd and popd commands. They let you do stack stuff with current dir.

  2. Yes, and with the modified CD command you can also use them. EX: “CD C:code; popd” will put you back were you started. However with pushd and popd you can’t jump to the 5th directory back (or forward) because they don’t maintain history after being popped. You can only go one directory (back) at a time, and you can’t go forward.

    I was also made aware of a modified CD command in the http://pscx.codeplex.com that’s worth checking out.

  3. In the curent implementation you can’t. I did think about that case, but can’t recall the last time I needed to do that, and figured I would add an exception for that case if/when it came up.

Comments are closed.