208

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

How do you set PowerShell's default working directory?

3

18 Answers 18

303

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.

Sign up to request clarification or add additional context in comments.

15 Comments

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.
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"
If that's a bad option, is there a better/safer way to do it?
Let me correct myself. When writing powershell scripts, e.g. for build, always use -NoProfile option like powershel -NoProfile -File "myscript.ps1"
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.
|
93

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 Comments

For PS Core: pwsh.exe -WorkingDirectory C:\YourLocation\Goes\Here
"C:\Program Files\PowerShell\6\pwsh.exe" -WorkingDirectory D:\test
I used powershell.exe -NoExit -command "& {Set-Location C:\my\path}"
56

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

3 Comments

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).
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
You can also add a shortcut that would launch PowerShell straight from the desktop. Mine is set to Ctrl + Alt + p
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.

3 Comments

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/…
That's weird, cause it works here. Just tested with changing the dir and changing Run as aministrator checkbox back and forth.
easiest solution - works perfectly in windows 11
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 Comment

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

this seems to be a less-complete version of the Create a PowerShell profile as follows. Answer. what is better about yours?
6

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 Comments

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/…
This is the best solution (for me, at least).
That is why the scripts should use the -NoProfile option like powershell -NoProfile -File "myscript.ps1"
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 Comment

This is an extremely useful way of having different locations / commands set inside a function. Makes it easier to navigate multiple 'sessions'
6
Write-Output "Set-Location C:\" >> $profile

Comments

5

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

Comments

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

Comments

3

Using graphical interface is completely enough for this:

  1. Open PowerShell
  2. Open Preferences as a new tab enter image description here
  3. Preferences enter image description here

3.1. Profiles -> default profile

3.2. Expand the 1st row

3.3. Select directory you need

3.4. SAVE new preferences.

Sorry for cyrilic on pictures (this is my system interface) but the solution is the same.

Comments

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 Comments

So how do we set the working directory when running powershell.exe (without a GUI shortcut)
[Environment]::CurrentDirectory = $env:userprofile for example although this should matter only when using .Net methods like [System.IO.File]::ReadAllText() which do not use the current location
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)

Comments

2

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 Comment

That is very usefull and the answer should be accepted
1

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

Comments

0

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 Comments

[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.
Not sure what you are using, but here it is: i.imgur.com/5W0xLJK.jpg
that is not powershell ... it's the "newish" windows terminal. [grin]
0

One important item to remember, often user's want to execute commands without the .\ preface, even if the working folder is active and current, it will fail - it should be a member of the system PATH environment variable to achieve the desired results.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.