Is there a way to change PowerShell\'s default location?
How do you set PowerShell\'s default working directory?
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).
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)
Write-Output "Set-Location C:\" >> $profile
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.
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
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