186

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

How do you set PowerShell's default working directory?

3

16 Answers 16

276

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.

12
  • 4
    Run this: "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned" You can read about it here: 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, 2017 at 7: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 7, 2017 at 14:23
  • 2
    If that's a bad option, is there a better/safer way to do it?
    – Adam_G
    Apr 17, 2017 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, 2017 at 12:19
  • 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. Apr 5, 2019 at 16:50
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.

3
  • 10
    For PS Core: pwsh.exe -WorkingDirectory C:\YourLocation\Goes\Here Dec 2, 2019 at 10:44
  • "C:\Program Files\PowerShell\6\pwsh.exe" -WorkingDirectory D:\test
    – Cem Kaan
    Mar 26, 2020 at 6:36
  • 2
    I used powershell.exe -NoExit -command "& {Set-Location C:\my\path}"
    – Gabrielius
    Feb 2, 2022 at 13:42
48

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.

4
  • 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 stackoverflow.com/questions/31622469/…
    – Santhos
    Apr 7, 2017 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, 2017 at 9:40
  • Works a charm. Thanks. Oct 3, 2017 at 4:24
  • easiest solution - works perfectly in windows 11 Aug 24, 2022 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

2
  • 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, 2020 at 0: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 Oct 21, 2020 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

1
  • 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 at 11:05
7
Write-Output "Set-Location C:\" >> $profile
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).

1
  • This is an extremely useful way of having different locations / commands set inside a function. Makes it easier to navigate multiple 'sessions' Nov 4, 2020 at 6: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

1
  • 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, 2021 at 8: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.

3
  • 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 stackoverflow.com/questions/31622469/…
    – Santhos
    Apr 7, 2017 at 14:29
  • This is the best solution (for me, at least). Aug 6, 2017 at 19:13
  • That is why the scripts should use the -NoProfile option like powershell -NoProfile -File "myscript.ps1"
    – Santhos
    Aug 31, 2017 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

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

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
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.

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)
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
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.

3
  • 2
    [1] there is no down arrow on my powershell.exe console window. what are you talking about? ///// [2] there is no such thing in the ISE either.
    – Lee_Dailey
    Jan 9, 2022 at 9:22
  • Not sure what you are using, but here it is: i.imgur.com/5W0xLJK.jpg
    – MrAWD
    Jan 10, 2022 at 14:04
  • 1
    that is not powershell ... it's the "newish" windows terminal. [grin]
    – Lee_Dailey
    Jan 10, 2022 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.