Welcome PowerShell User! This recipe is just one of the hundreds of useful resources contained in the PowerShell Cookbook.

If you own the book already, login here to get free, online, searchable access to the entire book's content.

If not, the Windows PowerShell Cookbook is available at Amazon, or any of your other favourite book retailers. If you want to see what the PowerShell Cookbook has to offer, enjoy this free 90 page e-book sample: "The Windows PowerShell Interactive Shell".

1.16 Launch PowerShell at a Specific Location

Problem

You want to launch a PowerShell session in a specific location.

Solution

Both Windows and PowerShell offer several ways to launch PowerShell in a specific location:

  • Explorer’s address bar

  • PowerShell’s command-line arguments

  • Windows Terminal “Open in Windows Terminal” shell extension

Discussion

If you are browsing the filesystem with Windows Explorer, typing pwsh.exe or powershell.exe into the address bar launches PowerShell in that location (as shown in Figure 1-2).

wps4 0102
Figure 1-2. Launching PowerShell from Windows Explorer

Note that what you type must end with the .exe extension, otherwise Explorer will generally open your PowerShell documents folder. Additionally, you can open Windows PowerShell directly from the File menu, as shown in Figure 1-3.

For another way to launch PowerShell from Windows Explorer, Windows Terminal (if you’ve installed it) adds an “Open in Windows Terminal” option when you right-click on a folder from Windows Explorer.

If you aren’t browsing the desired folder with Windows Explorer, you can use Start→Run (or any other means of launching an application) to launch PowerShell at a specific location. For that, use PowerShell’s -NoExit parameter, along with the -Command parameter. In the -Command parameter, call the Set-Location cmdlet to initially move to your desired location.

pwsh -NoExit -Command Set-Location 'C:\Program Files'
wps4 0103
Figure 1-3. Launching PowerShell from Explorer