次の方法で共有


Replacing CMD with PowerShell

First off, I really like Windows PowerShell.  I have been using it for a while now and I find it extremely powerful.  I now use it for every command line thing that I do.  (I would also recommend the PowerShell Community Extensions.  These add some very useful features to PS.  One that I particularly enjoy is that they integrate the Visual Studio path so that I can do everything from PS without having to open a VS Command Prompt).

So of course, how cool would it be if I could do the "Start->Run->cmd" thing (or Windows+R->cmd.exe) and get PS instead of the typical Windows command prompt?  This keystroke combination is forever embedded in my head, as I am sure it is the same for most of you.

Well, after doing a little research about "Image File Execution Options", I thought that I was on to something.  IFEOs are typically used to attach a debugger to a process so that when the process is started, the debugger launches first and attaches itself to the process.  Process Explorer uses this same "feature" to replace Task Manager and start PE instead.  Why not try this with PS?  To see how PE did it, I looked at the following registry key:

 

Under the "Image File Execution Options" key, a key was created with the name of the process to alter (in this case "taskmgr.exe").  The under the "taskmgr.exe" key, a new string value was created named "Debugger" that had a value of the path to the Process Explorer executable.

OK, so what if I did the same thing for "cmd.exe" and set the "Debugger" value to the path for PowerShell?  Well, it worked...PS started when I ran "cmd.exe" from the Run dialog.  HOWEVER, it then started "cmd.exe" inside of PS, so I lost the PS functionality and was reduced to the much depreciated Windows command line environment. 

After looking at the PS process in Process Explorer, I found that the command line executed to start the process was "c:\...\powershell.exe c:\...\cmd.exe".  So the IFEO passes the original executable as an argument to the replacement "debugger".  This makes total sense (and I expected that going into all of this, but this verified it).

So how can I get PS to load without starting cmd.exe?  The solution that I use is simple.  I created a .NET executable called "PSHelper.exe" that just starts a new process from the first command line argument that is passed to it.

 [STAThread]
static void Main(string[] args)
{
  if (args.Length > 0)
  {
    string path = args[0];
    if (File.Exists(path))
    {
      Process.Start(path);
    }
  }
 }

Then I set the "Debugger" value in the IFEOs for cmd.exe to "c:\...\PSHelper.exe c:\...\powershell.exe".  And now, when the cmd.exe process is started, we get the much more powerful PowerShell instead!

Side Note:   I have not done the research to see if this could possibly cause problems with the OS (for example if something relies on some certain feature of cmd.exe to properly function).  This post was more for informational purposes and some late night tweaking fun.

Comments

  • Anonymous
    September 09, 2007
    The comment has been removed

  • Anonymous
    October 02, 2008
    The comment has been removed

  • Anonymous
    March 26, 2009
    Thanks guys for the tips. I followed Jaykul's way and it worked... but cmd.exe isnt replaced completely... hmm, shouldn't be more simple just replacing the cmd.exe-cutable file?

  • Anonymous
    May 08, 2009
    You could always use SlickRun, no registry mods.  Create a new magicword called cmd and point to powershell.

  • Anonymous
    March 28, 2011
    ====== !!! Read this solution !!! This helped me!!! ======== I tryed to use "Joel "Jaykul" Bennett "'s method. That's worked for me. But theres one interesting thing i understood. PowerShell's blue interface is just the configuration of the label to the PowerShell, if you'll try to start powershell from its directory, it's just shows us black screen just like cmd's interface is. this feature can confuse people and that's why username @diegocr thought that this is not the right way. All you need is just copy system's label to the powershell into the powershell's directory and do all steps of the  "Joel "Jaykul" Bennett "' method for this label. then when you'll type cmd in the RUN dialog, you will se the native blue screen of the Powershell, =============================================== thank you guys, you all awesome.

  • Anonymous
    March 31, 2011
    My fixed solution. You cannot setup those steps for the ink file. Powershell is starting in its normal style, but you cannot pass there strings in arguments. Do all this like in that guy's method. but when you will start Run->cmd->enter for the first time. just click on the powershell's icon (on the left-top corner or the window) and choose "defaults". then open properties of the powershell's ink file (you can find it in the system menu in the search bar by typing "powershell") clicking on it by the right mouse button. make all configuration similar to the properties of the powershell's ink file. then you can use cmd as powershell and it's gonna start everytime with its native style

  • Anonymous
    March 04, 2014
    The comment has been removed