Is there a way to change PowerShell's default location?
How do you set PowerShell's default working directory?
Is there a way to change PowerShell's default location?
How do you set PowerShell's default working directory?
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 named WindowsPowerShell
for classic PowerShell or PowerShell
for newer PowerShell Core. If it does not exist, that's ok; just create it.
profile.ps1
in the WindowsPowerShell
folder (or PowerShell
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.
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.
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.
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.
An easier way to set the default directory is the following:
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)
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.
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).
Open file Microsoft.PowerShell_profile
under C:\Users\yourusername\Documents\PowerShell
Add the following line:
set-location "C:\Whatever\path\you\want\to\set\as\worrkingdir\"
Relaunch PowerShell
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.
If what you want is to open powershell from windows terminal in the current directory, this worked for me:
Now, if I'm in a directory and hit:
It opens powershell from windows terminal in the current directory
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 ~)
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
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
.
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)
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
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.