186

Is there a way to change PowerShell's default location?

How do you set PowerShell's default working directory?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Youbloodywombat
  • 2,087
  • 2
  • 13
  • 10

16 Answers16

275

Create a PowerShell profile as follows.

  1. Run PowerShell as administrator and execute the following command:

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

    This will permit PowerShell to run local scripts and scripts downloaded from the Internet that have been signed. Read more about this command in the documentation.

  2. In your Documents folder, find a folder named WindowsPowerShell for classic PowerShell or PowerShell for newer PowerShell Core. If it does not exist, that's ok; just create it.

  3. Create a new file named profile.ps1 in the WindowsPowerShell folder (or PowerShell for PowerShell Core).
  4. Open profile.ps1 and add the following command to set your default working directory:

    Set-Location C:\my\default\working\directory
    
  5. Open a new PowerShell window... the changes should have taken effect.

DeepSpace101
  • 13,110
  • 9
  • 77
  • 127
Jeremy Danyow
  • 26,470
  • 12
  • 87
  • 133
  • 1
    I tried this, but get "profile.ps1 cannot be loaded because running scripts is disabled". Is there "safe" way to enable safe scripts? – John Little Mar 04 '17 at 15:55
  • 4
    Run this: "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned" You can read about it here: https://technet.microsoft.com/en-us/library/ee176961.aspx It does not allow downloaded scripts from the internet to run unless they come from a trusted source. – LLL Mar 15 '17 at 07:12
  • 11
    This is a really bad way to do it because if you have a build script for instance (like visual studio build script) and you want to run a ps command, then the command's working directory would be set to that location, so it will basically break all scripts using powershell like `powershell -File "myscript.ps1"` – Santhos Apr 07 '17 at 14:23
  • 2
    If that's a bad option, is there a better/safer way to do it? – Adam_G Apr 17 '17 at 20:23
  • 10
    Let me correct myself. When writing powershell scripts, e.g. for build, always use -NoProfile option like `powershel -NoProfile -File "myscript.ps1"` – Santhos Aug 31 '17 at 12:19
  • This not only works, it also works for the Admin shell, which is what I needed. +10 if I could. – Wyrmwood Sep 21 '18 at 16:49
  • This should be the accepted answer, since it works no matter what method you use to open PowerShell. – mbomb007 Mar 08 '19 at 15:35
  • 4
    The `Set-Location` directive worked for me, but the name of the profile file needed to be different. If you type `$Profile` in PowerShell, you'll get the name and location of the profile file PowerShell is using. Then type `vim $Profile` (or whichever editor you use) to edit that same file. – Greg Kaleka Apr 05 '19 at 16:50
  • If you do it this way, any programs that open powershells in specific directories (e.g. Visual Studio Code's integrated terminal) won't be in the directory where you'd expect them to be; i.e. this overrides any command-line arguments. – darda Apr 27 '21 at 19:57
  • More on `pwsh` init script: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7.2#the-profile-files – Naramsim Nov 15 '21 at 16:24
  • This worked for me in Windows 10 with Visual Studio Developer PowerShell. – Mike Cheel Sep 01 '22 at 18:06
  • 1
    Ty sir. This will save me so much time! Works as instructed. – Dan Bechard Mar 01 '23 at 21:28
87

You could specify the directory to open when starting PowerShell:

powershell.exe -NoExit -command "& {Set-Location $env:systemroot}"

Just use it in your shortcut.

Or use a profile to set a start directory.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peter Hahndorf
  • 10,767
  • 4
  • 42
  • 58
47

I had tried the above answers in Windows Server 2016 without success.

But I found this approach (it should be the same for Windows 10) working for me.

  1. Start a PowerShell session
  2. In the Taskbar, right-click and pin to keep a link there
  3. Again right click the icon in taskbar and then right-click Windows PowerShell and choose Properties
  4. Enter your preferred directory in the Start in: input field and press OK
  5. Start from the taskbar icon

Done!

In the same Properties dialog you can also change many other settings like fonts, colors, sizes and on the Shortcut tab there via button Advanced. You can select if that PowerShell session is to be run with administrator privileges.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
neongrau
  • 933
  • 9
  • 12
  • 5
    Changing the working directory does not seem to work when you check `Run as administrator` in the `.lnk` advanced menu. The solution seems to be here http://stackoverflow.com/questions/31622469/run-as-administrator-changes-batch-file-current-directory-sometimes – Santhos Apr 07 '17 at 14:30
  • That's weird, cause it works here. Just tested with changing the dir and changing `Run as aministrator` checkbox back and forth. – neongrau Apr 10 '17 at 09:40
  • Works a charm. Thanks. – Yasser Shaikh Oct 03 '17 at 04:24
  • easiest solution - works perfectly in windows 11 – Amine Gaaliche Aug 24 '22 at 18:17
43

An easier way to set the default directory is the following:

  1. Right click the Windows PowerShell icon and pin to Start
  2. Right click the Windows PowerShell icon in Start, and again right click Windows PowerShell and select Properties (not Run as Administrator and not Windows PowerShell ISE)

    Enter image description here

    1. In the Shortcut tab -> 'Start in' field, change to the location you want PowerShell to start in.

    Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Antonio
  • 689
  • 6
  • 4
  • Ooh thank you, I like this answer. Helped me understand why and which directory was being set as the "default." I think this solution also slightly reduces the overhead time-cost of loading the profile (not sure if it is true or not, but it feels like it). – Jennings Jan 15 '20 at 00:15
  • 1
    this is a much better solution if you also use powershell within VS code as it allows the default there to be whatever folder you are currently working in versus a sort of global default like the above answers – codeAndStuff Oct 21 '20 at 22:58
20

Type this in PowerShell:

New-Item -path $profile -type file –force

It creates a .ps1 file in the PowerShell folder. Open it, and edit it as:

Set-location C:\files

Done

Refer to this link. It works fine.

Change PowerShell Start Directory

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rochester
  • 311
  • 2
  • 3
  • I think this answer is better because not everyone's powershell profile directory is the same as "C:\\Users\Document\...", for example mine is quite different. The first line of command will output your profile directory in the shell. – ShuangSong Jun 22 '23 at 11:05
7
Write-Output "Set-Location C:\" >> $profile
MarredCheese
  • 17,541
  • 8
  • 92
  • 91
Dave
  • 371
  • 3
  • 3
6

Instead of unconditionally changing the working directory as mentioned in previous answers, you can write a simple function in the PowerShell profile to use Set-Location to quickly change the working directory whenever necessary.

Check Jeremy Danyow's answer to create/modify a PowerShell profile.

Add a function(s) to your PowerShell profile:

function goto_this {set-location 'your\path\to\some\dir'}
function goto_that {set-location 'your\path to some\dir with space'}

Just change the function name and directory pointed to. Using quotes on the path is mandatory if it contains spaces. I try to keep the prefix goto_ as it helps in remembering the functions' names.

You can start typing goto_ then press TAB to cycle through all the added functions (remember to start a new PowerShell window after adding/modifying functions).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sundhar
  • 61
  • 1
  • 2
  • This is an extremely useful way of having different locations / commands set inside a function. Makes it easier to navigate multiple 'sessions' – CvRChameleon Nov 04 '20 at 06:55
6
  1. Open file Microsoft.PowerShell_profile under C:\Users\yourusername\Documents\PowerShell

  2. Add the following line:

    set-location "C:\Whatever\path\you\want\to\set\as\worrkingdir\"
    
  3. Relaunch PowerShell

MarredCheese
  • 17,541
  • 8
  • 92
  • 91
Ahmed Numan
  • 81
  • 1
  • 3
  • this seems to be a less-complete version of the `Create a PowerShell profile as follows.` Answer. what is better about yours? – Lee_Dailey Jan 26 '21 at 08:11
5

Putting Set-Location into your profile will unconditionally change the current working directory, which might have unwanted consequences in regards to the working directory for scripts that you execute via "run with PowerShell".

An alternative solution is to change the working directory for the .lnk files to PowerShell usually found in %USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell. Right click on a link, and change the working directory from %HOMEDRIVE%%HOMEPATH% to the directory you want.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Leonard Brünings
  • 12,408
  • 1
  • 46
  • 66
  • 1
    Changing the working directory does not seem to work when you check `Run as administrator` in the `.lnk` advanced menu. The solution seems to be here http://stackoverflow.com/questions/31622469/run-as-administrator-changes-batch-file-current-directory-sometimes – Santhos Apr 07 '17 at 14:29
  • This is the best solution (for me, at least). – Anders Arpi Aug 06 '17 at 19:13
  • That is why the scripts should use the `-NoProfile` option like `powershell -NoProfile -File "myscript.ps1"` – Santhos Aug 31 '17 at 12:20
4

If what you want is to open powershell from windows terminal in the current directory, this worked for me:

  1. Select defaults
  2. Adding . as starting directory

Now, if I'm in a directory and hit:

  1. alt key + d (it selects the path in windows explorer)
  2. type wt (it replaces the selected path with wt)
  3. hit enter

It opens powershell from windows terminal in the current directory

enter image description here

Rauland
  • 2,944
  • 5
  • 34
  • 44
3

In windows 11 I could fix this by setting the directory in the shortcut properties. Right click on Powershell in the taskbar, select properties and change the WorkingDirectory flag (default it was set to ~)

enter image description here

Steven Delrue
  • 423
  • 5
  • 8
2

Using just the command line, if a file exists already it will append to it:

$(if (-Not (Test-Path ~\Documents\WindowsPowerShell\)){ mkdir ~\Documents\WindowsPowerShell\}) ; echo "Set-Location c:\THELOCATIONYOUWANT" >> ~\Documents\WindowsPowerShell\profile.ps1
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Luke Angel
  • 41
  • 3
2

With that, there seems to be some confusion on the "working directory" and PowerShell's "location". What most people here are doing, and saying to do is change PowerShell's "location". The "working directory" is actually different. Here is an article that explains it.

For those who don't want to read the article: Open PowerShell and use what others have said to do Set-Location "C:\some\directory". Notice that your "working directory" is still where your PowerShell was opened at. Either "~" or "%SYSTEMROOT%\system32" depending on if you ran as administrator or not. To check the working directory, use [Environment]::CurrentDirectory.

Note: in the article the author says to check the "working directory" by using this command:

\[Environment\]::CurrentDirectory

I am not sure if this works with older PowerShell versions, but with PowerShell 5 (and later) you have to use [Environment]::CurrentDirectory.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RangerGeof
  • 21
  • 2
2

This solution sets current working folder to location where script is located. Be sure to place at beginning of your script, or at least before you try to use command relying on location path.

Set-Location (Split-Path $MyInvocation.MyCommand.Path)
Ipse
  • 186
  • 1
  • 3
1

Make this the first line in your Profile.ps1 and PowerShell Core (pwsh) will open in the directory you are currently working in:

set-location (get-location).path
MarredCheese
  • 17,541
  • 8
  • 92
  • 91
tedro
  • 51
  • 1
  • 5
1

Simplest way is to open Windows Powershell and click on the down arrow in the title bar to go to the Settings (you can use Ctrl+, as well). Make a window wider so you can see all the Profiles on the left side. Click on Windows Powershell profile and set your startup directory. Click Save at the bottom and you are done.

MrAWD
  • 27
  • 2