Open Powershell in a specific directory from shortcut

PowershellWindows 7Shortcut

Powershell Problem Overview


This sounds like it should be so simple... I must be dumb.

All I want is to make a windows short-cut that opens Powershell into a specific directory:

I'm using the target:

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

Put it just spits out the command as text.

Powershell Solutions


Solution 1 - Powershell

Use this command.

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

-NoExit: Do not exit after running startup commands.

Solution 2 - Powershell

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

Solution 3 - Powershell

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}"

Solution 4 - Powershell

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

Solution 5 - Powershell

try:

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

Solution 6 - Powershell

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.

Solution 7 - Powershell

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.

Solution 8 - Powershell

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'"

Solution 9 - Powershell

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

Solution 10 - Powershell

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

pwsh -WorkingDirectory "C:\path\to\your\directory"

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
QuestionDave BishView Question on Stackoverflow
Solution 1 - PowershellLoïc MICHELView Answer on Stackoverflow
Solution 2 - PowershellShay LevyView Answer on Stackoverflow
Solution 3 - PowershellDave BishView Answer on Stackoverflow
Solution 4 - PowershellGtdDevView Answer on Stackoverflow
Solution 5 - PowershellCB.View Answer on Stackoverflow
Solution 6 - PowershellΩmegaManView Answer on Stackoverflow
Solution 7 - Powershellfrank tanView Answer on Stackoverflow
Solution 8 - PowershellScott DimondView Answer on Stackoverflow
Solution 9 - PowershellgiluView Answer on Stackoverflow
Solution 10 - PowershellNishith SavlaView Answer on Stackoverflow