12

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.

Xenostar
  • 165
  • 2
  • 3
  • 11
  • possible duplicate http://stackoverflow.com/questions/14233659/open-powershell-in-a-specific-directory-from-shortcut – Loïc MICHEL Aug 13 '14 at 06:01
  • 1
    I wasn't specifically asking to have it start in one directory, but more along the lines of setting up shortcuts to get to different directories on the fly. So I don't consider this a duplicate. – Xenostar Aug 13 '14 at 15:40
  • What do you think about the part in my answer about using environment variables to set up shortcuts? – briantist Aug 15 '14 at 00:34

6 Answers6

21

There is also another way to do this:

function PP { cd C:\Users\Username\Dropbox\Websites\2014\Projects\ProjectName }

Set-Alias shortcut `PP`
FvD
  • 3,697
  • 1
  • 35
  • 53
eagle.one
  • 211
  • 2
  • 3
  • 3
    This is the best answer I've found so far for people like me used to bash. I'm not used to `cd`'ing into an alias, but rather used to calling the alias to take me to a folder. – FvD May 22 '17 at 23:21
  • 8
    The Set-Alias is not required. You can just type 'PP' to execute the function – John Burley Oct 20 '17 at 23:02
15

new-psdrive is your friend :

New-PSDrive -Name docs -PSProvider FileSystem -Root "C:\Users\username\Documents"
cd docs:
Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103
14

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.

briantist
  • 45,546
  • 6
  • 82
  • 127
  • 1
    This was the most helpful answer to me. From this I basically learned about setting up profiles in Powershell, which has really opened a whole new realm of possibilities for me now with setting alias and other random settings to get the shortcuts I desire. Thank you! – Xenostar Aug 15 '14 at 15:57
  • When I run `start notepad $profile`, Notepad gives me an error saying `The system cannot find the path specified`. – chipit24 Apr 02 '16 at 15:43
  • @cornflakes24 it still works for me. Make sure you're running that from powershell, not the windows command prompt. – briantist Apr 02 '16 at 16:27
  • 1
    @cornflakes24 You will have to create the profile, use `New-Item -Path $PROFILE -Type File` as described [here](http://www.howtogeek.com/126469/how-to-create-a-powershell-profile/) – RatherBKnitting Apr 09 '16 at 00:27
  • 1
    recommend `ise $profile` to launch the "Integrated Scripting Environment" for Powershell (gives you highlighting and code completion) instead of notepad – Mike Jan 16 '20 at 16:29
1

I believe this is the documentation you are looking for to change the default location where powershell starts up: The Windows PowerShell Profile

Mick Pletcher
  • 83
  • 2
  • 6
1

Although it doesn't directly answer the question, I found using this utility makes navigating around in powershell far easier: https://github.com/rupa/z

Install using Install-Module z -AllowClobber

Then navigate with z whatever where it will match "whatever" against your most frequently visited folders.

Simon Lang
  • 40,171
  • 9
  • 49
  • 58
-2

If the directory you want to go is named X; then write cd X:\

Bra
  • 1