10

I would like, by default, for PowerShell to be launched instead of Command when, say, running a batch file, or when I "Open Command Window Here", etc.

Any suggestions?

1
  • 5
    I don't think powershell can interpret batch files, so you probably shouldn't make it open those. Commented May 28, 2011 at 2:52

2 Answers 2

9

I just did a very quick test and the following registry entry will add a "PowerShell here" item to the context menu of all folders in explorer:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\PowerShellHere]
@="&PowerShell here"

[HKEY_CLASSES_ROOT\Directory\shell\PowerShellHere\command]
@="cmd.exe /c start \"PS\" powershell.exe -NoExit -Command \"cd \\\"%1\\\"\""

Just import it into the registry. I am using cmd.exe to bootstrap powershell in order to avoid having to hard-code the path.

Regarding cmd scripts/batch files. I think it does not make much sense to run CMD scripts within PowerShell by default. But it might be possible as well by modifying HKCR\cmdfile\shell\open registry key

1
  • Thanks! I'll give these a try tonight. The only reason I want scripts to open in powershell is for consistency and cooperation with my windowing tools. You are right that there is no inherent practical reason. ;) Commented May 28, 2011 at 0:30
0

You're not going to get very far with running CMD batch files in PowerShell.

You can try changing the %COMSPEC% variable to point at powershell.exe using setx, but I don't think it will take you long to realise that this is a really bad idea and change it back again.

save the file as *.ps1 instead of *.bat and it will use powershell

4
  • 1
    Why is it a bad idea? Just curious. Commented Jun 14, 2011 at 16:31
  • @afrazier: I have not experimented with changing %COMSPEC% to a non-CMD-compatible shell, but I assume that changing it to PowerShell would result in .cmd/.bat files trying to execute in PowerShell, so they would fail. I have installed a lot of software which needed to use batch files, so doing that would be a bad idea. Also, I am a bit biased, as I use CMD all the time (actually, TCC, which is an expanded superset), for everyday file managing - I only use PowerShell for system stuff, although I have a lot more to learn, as it is so powerful in comparison. Commented Jun 14, 2011 at 18:26
  • 1
    Just for giggles, I've changed %ComSpec% to point to PowerShell on my Win7 system. The first couple .cmd files I tested work just fine. They involve cmd-specific constructs (for, Variable manipulation, if), so PS is apparently smart enough to know when it needs to invoke cmd.exe. Commented Jun 14, 2011 at 18:37
  • For example, changing %ComSpec% to powershell breaks the mercurial version control commands. Commented Mar 19, 2019 at 13:46

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.