Friday, July 06, 2012

More on PowerShell as the Default Shell in Server Core 2012

Yesterday, I posted an article showing how easy it was/is to change your default shell inside Windows Server 2012’s Server Core installation option, and I’ve been playing around with it a bit more. I also noticed Paul Gregory’s blog post on the subject.

The shell that Windows Server 2012 uses to determine which shell to run is at: Hklm:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\winlogon" in the Shell item property.  In Server 2012, this is set, by default, to Explorer.exe. I’m still not quite sure how Explorer knows to load CMD.exe, but it seems to!

To change the shell is simple – just run a variation on: Set-ItemProperty -Path $RegPath -Name Shell -Value 'PowerShell.exe'.

By some variation – I mean you can, in the value entry, specify parameters to PowerShell at startup. If you just want a plain old shell, with all 4 profile files attempted, then leaving the Shell value to just “PowerShell.exe” is fine. But here are some other parameters you could also specify:

  • -MTA, –STA – you can specify to always start up PowerShell in a single threaded or multithreaded apartment. –STA is the default, but if you always need an MTA on a given server, you can.
  • -PSConsoleFile – you can create a console by configuring PowerShell the way you want it to run, then use Export-Console. This might be useful on a single function server to pre-load specific modules, but you’d need to work out how to manage the exported console (where do you put it, how do you update it, etc).
  • -NoProfile – speeds up startup, and is arguably a tad safer. You could theoretically get your profiles ‘infected’ – this option avoids such malware running.
  • -EncodedCommand – accepts a base-64 encoded string – useful if you have to pass a command to PowerShell that requires complex quoting, and use of braces ({}).
  • -Command, –NoExit – you can get PowerShell to run a command, over and above the profile files by specifiying the Command parameter. If you use the Command parameter, you should also specify –NoExit to avoid PowerShell from running the command and immediately exiting!
  • -Version – starts a specific version of PowerShell. This is useful to get PowerShell to start up in V2 on Server 2012, where V3 would of course be the default.

There are a few more parameters, but those are the key ones.

 

No comments: