Saturday, July 4, 2009

Change to Previous Working Directory in Powershell (cd -)

One of the cool features of linux bash is the ability to go to the previous working directory. In Bash, we can go to the previous working directory using "cd -" command.

Unfortunately, we do not have an equivalent of "cd -" in Powershell. So I just wrote a simple powershell cd function for this cool feature

Powershell CD function:

1function cd {
3 if ($args[0] -eq '-') {
4 $pwd=$OLDPWD;
5 } else {
6 $pwd=$args[0];
7 }
9 $tmp=pwd;
11 if ($pwd) {
12 Set-Location $pwd;
13 }
15 Set-Variable -Name OLDPWD -Value $tmp -Scope global;

Now lets see, how to install the above function in your powershell

Step 1: Remove Alias for cd

   cd is actually not a powershell cmdlet. It is an *alias* to Set-Location cmdlet. To install our new cd function, first we need to remove this alias

PS C:\Scripts> Get-Command cd

CommandType Name Definition
----------- ---- ----------
Alias cd Set-Location

PS C:\Scripts> Remove-Item Alias:cd
PS C:\Scripts>

Step 2: Install Powershell cd function

   We have removed the alias for cd. Now we can just go ahead and install our new powershell cd function which is very easy. Just paste the complete function in one line.

Powershell CD function in one line:

function cd { if ($args[0] -eq '-') { $pwd=$OLDPWD; } else { $pwd=$args[0]; } $tmp=pwd; if ($pwd) { Set-Location $pwd; } Set-Variable -Name OLDPWD -Value $tmp -Scope global; }

PS C:\Scripts> function cd { if ($args[0] -eq '-') { $pwd=$OLDPWD; } else { $pwd=$args[0]; } $tmp=pwd; if ($pwd) { Set-Location $pwd; } Set-Variable -Name OLDPWD -Value $tmp -Scope global; }
PS C:\Scripts>
PS C:\Scripts> Get-Item function:cd

CommandType Name Definition
----------- ---- ----------
Function cd if ($args[0] -eq '-') { $pwd=$OLDPWD; } else { ...

Ready to go, Lets see this cool feature in action

PS C:\Scripts> cd C:\Users\Jagadish\Desktop
PS C:\Users\Jagadish\Desktop> cd -
PS C:\Scripts> cd -
PS C:\Users\Jagadish\Desktop> cd ..\Documents
PS C:\Users\Jagadish\Documents> cd C:\Scripts
PS C:\Scripts> dir

Directory: C:\Scripts

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 6/6/2009 12:14 PM 279 test.ps1
-a--- 6/20/2009 8:27 PM 69 test1.ps1

PS C:\Scripts> cd -
PS C:\Users\Jagadish\Documents>

Hope you like it :)

Note: Powershell will not save any functions/aliases by default. So if you close your powershell and reopen it, this function will not be available. To make it permanent, it should be added in the Windows Powershell Profile file


  1. Great, except - how does one keep the cd alias deleted? When I delete it, the function works fine, but after closing the prompt instance, the alias is always back. How did you solve this? I suppose I could add the remove-item command to the profile, but that seems a little strange.

    1. Try naming the function something other than 'cd', like 'cddash' for example. Then include the function in your powershell profile and add the following line:

      set-alias -Name cd -value cddash -Option AllScope

  2. Well, yeah you are right. If you close and reopen the powershell, the alias is always back.

    Unfortunately, We don't have any other option except adding Remove-Item cmdlet to the profile file :(

  3. Good job, simple and elegant.

  4. Great job ! It is very simple and easy. Cd- is very simple command for changing previous working directory..

  5. This comment has been removed by the author.

  6. I have read so many rticles orr reviews concerning the blogger lovers except this piece of wrifing is really a fastidious article, keep itt up.

    my web page ... repossed cars land homes For sale in texas

  7. As a newbie who comes from UNIX to powershell I really love these kinds of clear, clean tips. This is great, Thanks !!

    One other way I found to save the function is to use the following
    1. Save function in a file. I called mine function_cd_previous.ps1
    2. Then run this on the commandline
    . ''
    Note the SPACE between the and the

    e.g. . 'c:\powershell\function_cd_previous.ps1'

    There may be issues doing it this way. I'm not sure. Just trying to add to the collective :)

  8. This is my version using pushd/popd:

    function cd-previous
    if ($args[0] -eq '-')
    pushd $args[0]

  9. I feel you look for something like Pop-Location ( and Push-Location (

  10. You can actually comment anonymously here.

  11. i really likes your blog and You have shared the whole concept really well. and Very beautifully written,
    soulful read! thanks for sharing.
    gclub casino
    goldenslot casino

  12. Online casino superior level.
    Our online casino. It's a widely spread web. Whether a gambler or a public interest in playing online casino. It is a website that offers services of joy. In the form of gambling games. With a variety of gambling games to choose from. There are also services. To cover the gambling style for you. More than imagined It is also the site with the largest number of participants to play online casino. With a high level of service responder Because the staff is there to help you with every problem you encounter 24 hours a day, without having to worry. Because only you join our site. Unless you are having fun then. You also have the opportunity to earn a fortune. Our website is a solid and reliable website. You can also access our website no matter where you are in the world. Do not wait too long Join us on our website.รูบี้888

  13. More Betting Games
    Goldclub SlotThe site is open to play online casino in more thorough. No matter what you do Where to play in any time does not matter. You can access online casinos, choose different playing styles. Which has a variety of games. Variety of choices Everytime, anywhere And for you to choose to join the fun. In each game, it will have different playing tricks. Many are useless and ineffective. Different in each game. Because of that, the decision to gamble is up to you to decide who will play in what direction. But even the gimmick and skill will vary. You have the same goal: play for the win and hope for a return home. But playing online casinos. Indeed There are simple ways to play. To point your fate, you will bring money back home or you will lose 100% even if all the casinos want to bring back the money back home. But sometimes miss a chance. Because playing online casinos if you throw out in order to play the game. That means you have the opportunity to be a jackpot recipient. But at the same time, if you throw out. But luck is not with you. That means you have to be 100% unconditional as well. So if you do not want to lose all in an instant. You need to plan a play that will not only give you a lot of reward But it will allow you to enjoy and relax with the various forms of play. Yes, every time you click on a website. คาสิโนออนไลน์

  14. ew casinos for you.
    Ruby888Online casino site Is a new online gambling game. You can access your mobile phone easily, quickly and safely. At the time you want At the time you want to relax and enjoy yourself. Without limiting whether you will ever play or not. We have games suitable for playmates. To a game that is suitable for gamblers. We have a style of play and how to play that will guide you. You do not have to worry. If you try to join us. You are entitled to receive a prize from our online casino. You do not have to travel far to casinos in foreign countries. We also have live broadcasts over high-speed internet that do not pose problems during your play or competition. And our online casino. High standards for high security. And feel free to join us. If you are interested in becoming a part of our online casino site today. You will have the privilege of having fun before anyone else. And is eligible for a grand prize of Jackpot Prize from each game. Try to be a part with us. Let's get to the website. IBCbet

  15. The online gambling web site you need to try.
    รอยัล1688Our website is an online casino site with high security standards. And standards in every aspect and is another channel for online gambling. The largest number of users in the casino industry today. The game of online gambling is considered to be very appreciated with a system of audio and video services that are as sharp and clear as the player to play gambling in the casino really makes the player. Great choice to play gambling games online through fun, it does not have any difference in any way. So it is not uncommon nowadays that most gamblers choose to gamble online, rather than playing games. And for this reason, it makes our online casino site. It became known and popularized. And not only that, playing all-in-one online betting is another open source online betting game. With the advent of the casino industry. The fun of playing online gambling games comes with a fun and realistic service. It allows all players to play online casino games every time you come to the service and play games online betting site. viva3388

  16. The fun of online casino games.
    Gclub SlotIs a g club website The leading online casino that is open to online gambling games that can win the hearts of gamblers because the casino gambling club is focused on playing gambling online standard. All online casino games are selected. And a good selection of online gambling games that are suitable for gamblers east. So nowadays, online casino websites are well-known and popular. Because today there are a number of gamblers who are quite selective to play casino games online through the Poipet website. The casino, because it is easy to access and play, and online casino games can also play in both mobile phones and on the computer. Gamblers can access and enjoy hundreds of online gambling games 24 hours a day, and you can always have fun at risk. I want to have a websiteHoliday Palace

  17. Online Sports Betting Sites
    Ruby888Our online casino site is an online gambling gaming website. The most comprehensive and fashionable in modern times. It has a very simple process and method. The player is easy to use. Including a novice player to play with. And no matter what you do at any time, you can access unlimited online casinos. Just have internet on smartphone. In addition, our online casino site has chosen to play different types. Developed from a real casino abroad. Come to our website that was developed for you without wasting your time orbiting away. And games with a lot of games. Variety of choices You can play anywhere, anytime. You can choose to play as you like. By the way, you will be determined to use various tips. In each game itself. In order to win each game and bring a pension back home. And if you plan to play games well. Not only will it keep you entertained in your spare time. You will also receive a large reward. And more special If your play has a problem, we have a 24 hour support staff that you do not need to worry about. And our staff will help you comfortably. You can enjoy our online casino and relax with the style of online gambling. Every time you click on us visit our website. IBCbet

  18. Gclub casino online casino Another way to wealth
    Gclub SlotIs a web g club An international online casino of choice for fun choices is another merry choice that has been given. The popularity is very much in the present. For this online casino site is considered a representative of Cambodia, the neighboring country of Thailand, which opened the gambling club. Online casinos for more than 10 years for the development of online gaming systems and patterns, this makes gamblers online. Then every eastern gambler can access and play online gambling more easily. Due to the high tech service system. Easy access to the site Poipet. The casino is known and is said in the casino. Is a g club website Casinos with the most live betting and online gambling games are also the most standard online gambling services, and they are also open to the public all day and night. Play online casino games every time you desire. When you think of online betting, you can come to the game of online games. Nine players like we have at the site. คาสิโน

  19. The best online gambling games.
    Ruby888Is a web g club The leading online casino that has been providing online gambling for more than 10 years for the fun and new opportunity to make online casino games more attractive due to playing games. Online gambling is another new way to add fun to the game, double the amount of gambling and more importantly, playing the game. Online is also an opportunity for new players can make money with fun is endless. Because of that, playing online gambling is another new joy. The gambler can choose to play online gambling more easily with just the Internet, only the gambler can play all online casino games that are available on the gambling website. This online casino. And most importantly, online gambling games on the computer screen can be played at any time the player wishes by just having an internet connection. Every time an internet gambler has an internet connection, the player can enter online gambling. All on the casino site. Our gambling clubs and major risk takers can also start enjoying and play casino games online with gamblers around the world on the webIBCbet

  20. High security standard casino
    บาคาร่าออนไลน์Online casino site In modern times Our website has made it easy for you. Because we are more modern and more thorough. No matter where you want to play our casino online, where and when it is not important because you have access to online casinos anytime easily just join us as a team with us. And you have the internet, you can access it on the phone. You can choose to play a variety of prototype, which has a lot of games. Variety of choices Everytime And for you to choose to join us. In each game, it will have different playing tricks. Many are useless and ineffective. Different in each game. But the decision to play all the games. Where are you going to be destined to be? But even the gambit and playing style are different. You have the same aim is to play to win and hope to return home. But playing online casinos. Indeed There are simple ways to play. To point your fate, you will bring money back home or you will lose 100%. You can come to the website. รูบี้888

  21. Online casino is open for a long time.
    Holiday Palace Is a betting site for all types of bets via the internet. Ready to serve you with expert criteria. Both the excellent service and the advice and recommendations as they really are for the customer with the right service is accurate. And the stable financial system is certainly acceptable. Customers can trust. With the best in control, both the service and financial stability are accessible and accessible. Answer every problem for the game of luck in the new age 4.0 in the digital age to use the Internet to provide comfort. For immediate, convenient, easy to generate money. Our G-club 69 internet betting site can provide you with advice and services. Take care of your lifestyle to be very satisfied. We decided to become part of this high-tech website with us and get the right kind of right to apply for a new team with a bonus of 30 percent for opening a minimum of 500 baht, up to 20000 baht. Do not miss out on this great opportunity to get started on how to make money online. Gclub

  22. Online Casinos With Great Games
    Gclub Slot Online casino site, one of the authority of online casino is famous. Online Betting Opportunities It is very easy to access. Because of the current online betting games, the player can join the excitement. Have fun and enjoy the sharp and clear picture and sound. And that is what makes online gambling games. On the web site of this online casino is very attractive and therefore, makes our online casino site became known and popular with Asian gamblers and very much. Our online casino site must be an opportunity to allow the player to access and play all online gambling games, not just on the website. Transportation to the site is filled with hundreds of reputable online betting services and players can play online bets. All forms are available upon request. You can join the fun and choose to play all online casino games at the website. Holiday Palace

  23. Online Slots Games Via the 69 club
    Royal1688Our website is the leader in providing online gambling services that include the most popular online gambling games. Baccarat And that is not the game is an Internet Slots. All of the fun that is in the online gclub69 we are available for you to play every day. The casino gambling services are many such as the Holiday Palace, Viva, Sbobet, Royal1688 and many more. We are ready to serve everyone. Speed up deposit within 5 to 10 minutes. Transparency in all games. Gambling. I do not know how it is, but at G-CLUB69 online casino. We have great deals for people to play all the time. Start with a new 100% Welcome Bonus on our site with a minimum of 400 sites up to 8000 Baht and we also have a great bonus. Online risk website is a force to play, do not wait that slow. Goldclub Slot

  24. Fun way to access the game.
    Ruby888Our online casino site is an online gambling game site. The most comprehensive and modern in the present. The procedure and how to use it. The player is easy to use. Including a new player to play with. And no matter what you do, wherever you are, you are able to access unlimited online casinos. Just have internet on smartphone. In addition, our online casino site has chosen a variety of play. Developed from a real casino abroad. Come to our website developed for you, you do not have to spend time to travel far. And games with a variety of games. Variety of choices You can play anywhere, anytime. You can choose to play as you like. You will decide in the use of various techniques. Each game has its own. In order to win each game and bring home the prize money. And if you plan to play the game well. Not only will you get fun in your spare time. You will also receive a large reward. And more special than that. If your play has a problem, we have a 24 hour support staff that you do not have to worry about. And our staff will help you. You can enjoy our online casino and relax with the style of online gambling. Every time you click on us visit our website. IBCbet

  25. How to eat dessert?
    Royal1688Our website has said that desserts are a favorite of all ages. Everyone can not dessert. But do not eat fat desserts. Today we have a good tip to tell the amount of calories. Before eating, read the amount of calories in the dessert. See the nutrition label on the box. If possible, choose to eat or buy the type of fat is less, reduce calories. Cut sugar cream from the pastry, such as spread the icing sugar over the bread. Or do not put coconut milk in the dessert. It reduces energy to 81-150 calories or spread the cake out. Reduces energy consumption by up to 160 calories. Eat enough to know the taste, such as 1-2 pieces of cookies, 1 part cake / 4 small pieces of ice cream, you can taste all the candy by half the calories. Drink green tea or hot coffee after snacking. Caffeine stimulates metabolism. To add flavor, add artificial sweeteners instead of 15 minutes after eating desserts, do not sit there. Take a walk around the house about 15 minutes, this way to help digest. 30 minutes after dessert 5 hours should exercise at least 30 minutes to get rid of flour and sugar before becoming fat accumulation in various parts of the body before and after exercise. The body should drink hot green tea or warm water to supplement the burned system. Coupled with calories Take the flour and sugar the next day. Breakfast is 80% protein, 20% protein. For dinner, eat fresh fruit and vegetables and drink healthy whole day. Thank you site. Royal1688

  26. Online casinos provide professional players.
    Ruby888The choice to open online casino games, betting on online casino games with a variety of innovative games. Gambling online is no fun. Players can risk their luck online at any time of the site. The online gambling casinos that are suitable for all types of gamblers. Gold to the merriment. All you have to do is play the game. And can meet all needs because of the online gambling games are open all the time. And the game to win a lotto online as you choose. More than 100 games to bring fun. Keep your eyes open. Online casino games and players will be more than entertainment. Who is online casino services? Make the deal and be popular. Never miss a new online betting game and also bring promotion. Privilege for new members and get more than 500 free credits. Every game is waiting for you. IBCbet

  27. Thank for your very good article.! i always enjoy & read the post you are sharing!


  28. จีคลับ A site that anyone can play. There are many games that will make it easy to play with the bets that will like to make good profits every day. Complete with unique bets. A fun betting platform offers great returns. Can play all areas. Make more money. Gambling Betting It provides fun and has a betting game to choose from. No time to bet. There are betting games like who wants to make a good income. There are gambling games like this. It can be played anywhere.

    Gambling gambling will provide comfort. Do not waste time on gamblers to provide line convenience. No need to go to the casino anymore. Just connect to the Internet, it can be played a lot of luck. Have a game that anyone can play very easily. Get a good reward every day. Gambling is simple. This is good money. Realistic game with international standards. Gambling everywhere. To play like. Ready to gamble to provide more benefits. Make good money. Make a substantial income every day. บาคาร่า