How do you set PowerShell's default directory?
WindowsPowershellWindows Problem Overview
Is there a way to change PowerShell's default location?
How do you set PowerShell's default working directory?
Windows Solutions
Solution 1 - Windows
Create a PowerShell profile as follows.
-
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.
-
In your
Documents
folder, find a folder namedWindowsPowerShell
for classic PowerShell orPowerShell
for newer PowerShell Core. If it does not exist, that's ok; just create it. -
Create a new file named
profile.ps1
in theWindowsPowerShell
folder (orPowerShell
for PowerShell Core). -
Open
profile.ps1
and add the following command to set your default working directory:Set-Location C:\my\default\working\directory
-
Open a new PowerShell window... the changes should have taken effect.
Solution 2 - Windows
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.
Solution 3 - Windows
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.
- Start a PowerShell session
- In the Taskbar, right-click and pin to keep a link there
- Again right click the icon in taskbar and then right-click Windows PowerShell and choose Properties
- Enter your preferred directory in the Start in: input field and press OK
- 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.
Solution 4 - Windows
An easier way to set the default directory is the following:
-
Right click the Windows PowerShell icon and pin to Start
-
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)
-
In the Shortcut tab -> 'Start in' field, change to the location you want PowerShell to start in.
Solution 5 - Windows
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.
Solution 6 - Windows
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).
Solution 7 - Windows
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.
Solution 8 - Windows
Write-Output "Set-Location C:\" >> $profile
Solution 9 - Windows
-
Open file
Microsoft.PowerShell_profile
underC:\Users\yourusername\Documents\PowerShell
-
Add the following line:
set-location "C:\Whatever\path\you\want\to\set\as\worrkingdir\"
-
Relaunch PowerShell
Solution 10 - Windows
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
Solution 11 - Windows
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
.
Solution 12 - Windows
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)
Solution 13 - Windows
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 ~)
Solution 14 - Windows
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
Solution 15 - Windows
If what you want is to open powershell from windows terminal in the current directory, this worked for me:
- Select defaults
- Adding . as starting directory
Now, if I'm in a directory and hit:
- alt key + d (it selects the path in windows explorer)
- type wt (it replaces the selected path with wt)
- hit enter
It opens powershell from windows terminal in the current directory
Solution 16 - Windows
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.