How start PowerShell from cmd by specific path
Solution 1
Try this:
powershell -NoExit -Command "Set-Location c:\\"
If your path contains spaces, you can wrap it in single-quotes, like so:
powershell -NoExit -Command "Set-Location 'c:\\program files'"
Solution 2
What Windows 10 uses in the Registry in order to open a PowerShell instance by shift-rightclicking in an Explorer window or on the Desktop ("Open PowerShell window here") is the following:
powershell.exe -noexit -command Set-Location -literalPath '%V'
This can be found at Computer\HKEY_CLASSES_ROOT\Directory\Background\shell\Powershell\command
Related videos on Youtube
Marslo
profile for Marslo on Stack Exchange, a network of free, community-driven Q&A sites http://stackexchange.com/users/flair/1152222.png?theme=dark
Updated on September 18, 2022Comments
-
Marslo over 1 year
I want to start PowerShell (C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe) from windows 7 default command line (cmd) and go to the specific path.
I used the command:
start C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe
It'll work, and show the powershell window.
But, if I use:
start C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe Set-Location c:\
The black window will flash quickly.
How can I open powershell from cmd by the specific path? Thanks in advance.
-
Admin about 11 yearsIf you must use the full path to
powershell.exe
, better to use%windir%
. -
Admin about 6 yearsOr
%SystemRoot%
-
-
Marslo about 11 yearsHi, I fix that. I using two strings,
s1 = 'start C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -NoExit -Command '; s2 = '"Select-Location ' + "'" + path1 + "'" + '"', and command = s1 + s2
. then it works -
Oliver Salzburg about 11 years@Marslo: You can also use
'
to wrap the path. Like:-Command "Set-Location 'C:\\Program Files'"
-
Marslo about 11 yearsYep! Thanks @Oliver Salzburg . :) I written this for gvim. I want to press F3(or other shortcuts) will show command line (Powershell) and cd in the current file path.
-
Karan almost 11 years@OliverSalzburg: Just found your answer while searching. Can you perhaps solve my closely related problem as well?
-
Thorsten Staerk about 3 yearsGreat answer, I just described this in a blog for people who are new to regedit: bingehacking.blogspot.com/2021/03/…