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.