How do you set PowerShell's default directory?

WindowsPowershell

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

  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.

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.

  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.

Solution 4 - Windows

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

  3. In the Shortcut tab -> 'Start in' field, change to the location you want PowerShell to start in.

    Enter image description here

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.

Change PowerShell Start Directory

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

  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

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

enter image description here

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:

  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

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYoubloodywombatView Question on Stackoverflow
Solution 1 - WindowsJeremy DanyowView Answer on Stackoverflow
Solution 2 - WindowsPeter HahndorfView Answer on Stackoverflow
Solution 3 - WindowsneongrauView Answer on Stackoverflow
Solution 4 - WindowsAntonioView Answer on Stackoverflow
Solution 5 - WindowsrochesterView Answer on Stackoverflow
Solution 6 - WindowsSundharView Answer on Stackoverflow
Solution 7 - WindowsLeonard BrüningsView Answer on Stackoverflow
Solution 8 - WindowsDaveView Answer on Stackoverflow
Solution 9 - WindowsAhmed NumanView Answer on Stackoverflow
Solution 10 - WindowsLuke AngelView Answer on Stackoverflow
Solution 11 - WindowsRangerGeofView Answer on Stackoverflow
Solution 12 - WindowsIpseView Answer on Stackoverflow
Solution 13 - WindowsSteven DelrueView Answer on Stackoverflow
Solution 14 - WindowstedroView Answer on Stackoverflow
Solution 15 - WindowsRaulandView Answer on Stackoverflow
Solution 16 - WindowsMrAWDView Answer on Stackoverflow