PoshCode Logo PowerShell Code Repository

cd command with history (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/697"></script>download | new post

I got this script originally from Jim Truher and have tweaked it a bit over time (nothing major). Basically it replaces your CD function with one that keeps a history.

  1. ########################################################
  2. # Custom 'cd' command to maintain directory history
  3. #
  4. # Usage:
  5. #  cd          no args means cd $home
  6. #  cd <name>   changes to the directory specified by <name>
  7. #  cd -l       list your directory history
  8. #  cd -#       change to the history entry specified by #
  9. #
  10. if( test-path alias:\cd ) { remove-item alias:\cd }
  11. $global:PWD = get-location;
  12. $global:CDHIST = [System.Collections.Arraylist]::Repeat($PWD, 1);
  13. function cd {
  14.         $cwd = get-location;
  15.         $l = $global:CDHIST.count;
  16.  
  17.         if ($args.length -eq 0) {
  18.                 set-location $HOME;
  19.                 $global:PWD = get-location;
  20.                 $global:CDHIST.Remove($global:PWD);
  21.                 if ($global:CDHIST[0] -ne $global:PWD) {
  22.                         $global:CDHIST.Insert(0,$global:PWD);
  23.                 }
  24.                 $global:PWD;
  25.         }
  26.         elseif ($args[0] -like "-[0-9]*") {
  27.                 $num = $args[0].Replace("-","");
  28.                 $global:PWD = $global:CDHIST[$num];
  29.                 set-location $global:PWD;
  30.                 $global:CDHIST.RemoveAt($num);
  31.                 $global:CDHIST.Insert(0,$global:PWD);
  32.                 $global:PWD;
  33.         }
  34.         elseif ($args[0] -eq "-l") {
  35.                 for ($i = $l-1; $i -ge 0 ; $i--) {
  36.                         "{0,6}  {1}" -f $i, $global:CDHIST[$i];
  37.                 }
  38.         }
  39.         elseif ($args[0] -eq "-") {
  40.                 if ($global:CDHIST.count -gt 1) {
  41.                         $t = $CDHIST[0];
  42.                         $CDHIST[0] = $CDHIST[1];
  43.                         $CDHIST[1] = $t;
  44.                         set-location $global:CDHIST[0];
  45.                         $global:PWD = get-location;
  46.                 }
  47.                 $global:PWD;
  48.         }
  49.         else {
  50.                 set-location "$args";
  51.         $global:PWD = pwd;
  52.                 for ($i = ($l - 1); $i -ge 0; $i--) {
  53.                         if ($global:PWD -eq $CDHIST[$i]) {
  54.                                 $global:CDHIST.RemoveAt($i);
  55.                         }
  56.                 }
  57.  
  58.                 $global:CDHIST.Insert(0,$global:PWD);
  59.                 $global:PWD;
  60.         }
  61.  
  62.         $global:PWD = get-location;
  63. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me