85

How can one make a windows short-cut that opens Powershell into a specific directory?

Such as the target:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe 
    -noexit -command {cd c:/path/to/open}

But that just spits out the command as text. How?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Dave Bish
  • 19,263
  • 7
  • 46
  • 63

13 Answers13

142

Use this command.

powershell.exe -noexit -command "cd c:\temp"

-NoExit: Do not exit after running startup commands.

DxTx
  • 3,049
  • 3
  • 23
  • 34
Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103
  • 22
    You will want to include `'`s if there are spaces in your path: `powershell.exe -noexit -command "cd 'c:\a path with spaces\readme.txt'"` – blachniet May 24 '13 at 12:03
  • Just what I was looking for. The additional quotes are necessary including when working with interpolated paths. – Aluan Haddad Jun 01 '15 at 12:36
  • 9
    If we're talking about _any_ sort of shortcut (as in, tips and tricks), there's this: if you are in the desired folder in Windows Explorer, you can type `powershell` in the address bar and it will open PowerShell at that location. This also works for `cmd` and any other application that Windows can find via the PATH environment variable. – Sameer Singh Jan 28 '16 at 11:10
  • Any idea why these solutions open PS in a black CMD style window instead of the standard blue PS window? – bubbleking Nov 22 '16 at 16:27
  • Thanks man, I used it to avoid using the terminal in Android Studio where the commands are cropped due to a bug. – Piero Jan 26 '17 at 08:28
  • the command works, but I get an error message to: The string is missing the terminator: ". + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString with %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "cd d:\" – tatmanblue Sep 28 '17 at 17:26
  • Try using single quote or revive the ending `\` – Loïc MICHEL Sep 29 '17 at 15:38
  • 1
    example C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit -Command Set-Location -LiteralPath "c:\tmp" – Gregory Bologna Feb 05 '21 at 19:05
45

You can also set the "Start in" shortcut field to your desired location.

Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • 2
    By far the easiest solution, especially if you're trying to use a path with spaces or odd characters (e.g. anything under Program Files (x86) ) – Henry Wilson Nov 01 '13 at 15:06
  • 20
    "Start in" didn't work for me, still started in c:\windows\system32. – Mattias Örtenblad Sep 17 '14 at 12:11
  • 16
    Yes, this doesn't work if the shortcut has "Run as administrator" checked. – Mica Jan 28 '16 at 07:07
  • 2
    It's even cooler when you leave the `Start in` field empty because then it opens in the current location. – t3chb0t Feb 04 '16 at 07:51
  • 1
    This is the solution that worked best for me. I made a copy of the existing shortcut, named it " - Vagrant", then set the "Start in" path to where my VagrantFile lived. Now I just hit the shortcut and "vagrant up" and can start working. – Caleb Mar 04 '20 at 18:16
10

Ok - you need to use the & parameter to specify it's a powershell comand & the syntax is slightly different:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe 
-noexit -command "& {cd c:\path\to\open}"
Dave Bish
  • 19,263
  • 7
  • 46
  • 63
  • Is there any difference between this and [the solution by Kayasak](http://stackoverflow.com/a/14233773/2707864)? – sancho.s ReinstateMonicaCellio Dec 18 '13 at 16:35
  • I am using PowerShell in [Hyper](https://hyper.is), in `.hyper.js`, I used this solution like: `shellArgs: ['-noexit', '& {cd "$HOME\\my old documents"}']`. I couldn't make `['-noexit', '-command', '"cd "$HOME\\my old documents"']` or `['-noexit', '-command "cd "$HOME\\my old documents"']` to work. – vulcan raven Jan 08 '18 at 06:27
  • 1
    old post but just helped me :) I was tired of getting stuck in system32 directory as an administrator. Thanks – Randy R Feb 21 '18 at 07:34
9

If you want powershell to start as admin and run in a specific directory, even on a different drive, it is better to use the Set-Location command. Follow these steps

  1. Create a ShortCutLink with the target being the powershellcommand exe.
  2. Leave Start in: blank. (Normally this starts in current working directory when blank; but we do not care.)
  3. Change Target to this with your targets for powershell and locations:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "Set-Location D:\_DCode\Main"

  1. Click Advanced... and select Run as administrator.
  2. Click OKs out.

Don't forget the handy trick to change the colors of the shortcut from the Colors tab. That way if you have two or more links which open powershell windows, seeing a different color can visually let you know which shell one is working in.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • This is the only one that worked for me -- thank you. For (my) future reference the full path to powershell.exe is `C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe` – wkille Oct 06 '21 at 10:33
9

Define a Shortcut for Powershell, and Open the properties of that, and finally in "Start" type the folder target to be opened when Powershell Shortcut is triggered

GtdDev
  • 748
  • 6
  • 14
6

try:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe 
-noexit -command "cd c:/path/to/open"
CB.
  • 58,865
  • 9
  • 159
  • 159
2

If one wants a explorer right click options run this script:

New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
if(-not (Test-Path -Path "HKCR:\Directory\shell\$KeyName"))
{
    Try
    {
        New-Item -itemType String "HKCR:\Directory\shell\$KeyName" -value "Open PowerShell in this Folder" -ErrorAction Stop
        New-Item -itemType String "HKCR:\Directory\shell\$KeyName\command" -value "$env:SystemRoot\system32\WindowsPowerShell\v1.0\powershell.exe -noexit -command Set-Location '%V'" -ErrorAction Stop
        Write-Host "Successfully!"
     }
     Catch
     {
         Write-Error $_.Exception.Message
     }
}
else
{
    Write-Warning "The specified key name already exists. Type another name and try again."
}

This is what is shown now:

enter image description here


Note that you can download a detailed script from how to start PowerShell from Windows Explorer.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
frank tan
  • 131
  • 1
  • 4
2

I use a .ps1 script file to open a PowerShell terminal at a specific path from a shortcut in the taskbar. The script:

cd 'directory path'
powershell

Running "powershell.exe" from a PowerShell terminal will start a new PowerShell session, preventing the terminal window from closing.

JFMoya
  • 93
  • 6
1

Copy this code into notepad and save with a reg extension. Double click the resulting file.If you get a message about importing to the registry click on yes and then ok. Navigate to any folder in explorer and bring up the context menu. This is typically done by clicking the right mouse button.


Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\PShell]
"MUIVerb"="Open in Powershell Window"

[HKEY_CLASSES_ROOT\Directory\Background\shell\PShell\command]
@="c:\\windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'"
1

If you prefer to launch Windows Terminal with your prefered command line shell, you can use:

wt.exe -d "c:\temp"
Troglo
  • 1,497
  • 17
  • 18
0

I just wanted to add my Developer Powershell link ... for the records.

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -noe -c "&{Import-Module """C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell d998f19b; cd c:\dev\}"

This will start the Developer Powershell (VS 2019) in c:\dev\.

gilu
  • 197
  • 2
  • 9
  • To open _this_ directory in Total Commander: add new button with this command `C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe` and these parameters: `-noe -c "&{Import-Module """C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell YOUR-VS-HEX-CODE; Set-Location '%P'}"` – gilu Jan 11 '22 at 06:30
0

If you are using Powershell 7 (pwsh), simply use the -WorkingDirectory flag like this:

pwsh -WorkingDirectory "C:\path\to\your\directory"
Nishith Savla
  • 310
  • 2
  • 10
0

If you are on a folder (File Explorer), you can write powershell on the address bar. This will open powershell that already moved to your current folder. The shortcut:

ctrl+L (move keybord to address bar)
powershell
Muhammad Yasirroni
  • 1,512
  • 12
  • 22