Preserving "Open With..." File Explorer context menu action while changing default starting directories in Powershell using Windows Terminal

Written on July 8, 2022

When you open Microsoft PowerShell in Windows Terminal on your computer, do you ever had the annoying feeling where you see your PowerShell session starts you off in a directory you do not want to start from? For me, at least, it would always start from C:\Windows\System32, and it’s a bit jarring for me.

The most common solution I have seen is to configure the Microsoft PowerShell profile with a Set-Location command:

# Replace "notepad" with any text editor you have installed.
notepad $profile

And add this command to your profile:

# Replace the location path with your desired absolute or relative path.
Set-Location "X:\my\path"

Here is a catch! After adding that command to your profile, all of your new PowerShell sessions in Windows Terminal will always redirect you to your X:\my\path, including your File Explorer context menu action. If you right-click on a folder and click on Open in Terminal, you will still get redirected to X:\my\path. How annoying is that?

Replace notepad command with other text editors in your shell, such as Visual Studio Code (code) or terminal text editors (emacs) if you have them installed. For now, we use notepad for the time being. Swap out X:\my\path with the desired drive and directory path you want.

Recently, I have found a way to solve this conundrum, using the Windows Terminal.

If you have already added the Set-Location command to your profile, you will want to delete it, since it is interfering with File Explorer’s ability to open Microsoft PowerShell to the current directory in Windows Terminal.

To start setting up, first you want to open up your Windows Terminal.

On the far right side of the toolbar menu, expand the dropdown, and open the Settings.

Down in the lower left corner, open the settings.json file by clicking on the gears icon.

Here, you will see a bunch of settings. You want to do a quick search using CTRL+F and find profiles.

In profiles, you have 2 properties, defaults and list.

In defaults, add or modify the property, startingDirectory, and set the value to be ".".

In any one of the Microsoft PowerShell JSON objects under list, add or modify the property, startingDirectory, and set the value to point to your desired X:\my\path location.

Now, whenever you right-click on a folder and open the folder in Windows Terminal, it will now correctly open your selected folder. And when you open a new tab to start a new Microsoft PowerShell session, you will now start in the directory, X:\my\path.

That’s how I solved it. Thanks for reading.