shellpowershellcommand-linecommand-line-interface

Windows Powershell: Shortcut for change directory


I just started using Windows Powershell and one major problem I've run into is when I start it, it starts me off in this directory:

C:\Users\Username

However, the directory I usually need to navigate to is in something like:

C:\Users\Username\Dropbox\Websites\2014\Projects\ProjectName

And sometimes it goes much deeper. So you can see how it is a little annoying to navigate each time I start the shell to this directory using ten separate cd commands. I was wondering if there was a way to set up a shortcut or alias for something like:

cd C:\Users\Username\Dropbox\Websites\2014\Projects\ProjectName

Or possibly I could set some sort of shortcut to the directory only so I could do something like:

cd shortcut

And it would cd to the proper directory. Does anyone have any experience with something like this? Is this a dumb thing to do? I'm very new to using any command line so I'm just trying to get used to navigating around files and folders more easily.


Solution

  • Run this in powershell:

    start notepad $profile
    

    That will open up your profile in notepad (notepad will prompt you to create it if it doesn't exist).

    Any code you write in this .ps1 file will be executed when powershell starts.

    You can also set a system environment variable, like if you were to set MYPSPATH equal to C:\Users\Username\Dropbox\Websites\2014\Projects then you could do this:

    cd $env:MYPSPATH
    

    That could be done either manually each time, or automatically within your $profile.

    Also it's unclear from your question, but it sounds like you're doing a cd for every path component.

    There's no need to do that. This command that you wished for:

    cd C:\Users\Username\Dropbox\Websites\2014\Projects\ProjectName
    

    will work as is. If I misunderstood this point I apologize.

    Something that also might be useful to you is pushd which is an alias of Push-Location. This lets you change to a new directory and easily revert back where you started with popd or Pop-Location.

    PS C:\users\Xenostar> Push-Location .\Dropox\Websites\2014\Projects
    PS C:\users\Xenostar\Dropbox\Websites\2014\Projects> Pop-Location
    PS C:\users\Xenostar>
    

    And you can push multiple levels deep, and keep popping back to the previous ones.