Tuesday, 18 September 2012

Disable-Gui.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script defines a function, Disable-Gui which 
  4.     disables the GUI on Windows Server 2012 Server Core 
  5. .DESCRIPTION 
  6.     The Disable-GUI function enables the GUI in Server 2012 
  7.     Server Core by Removing two windows features. The 
  8.     script add a Shell setting to ensure that when 
  9.     Server 2012 restarts, it starts with PowerShell.  
  10. .NOTES 
  11.     File Name  : Disable-GUI.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 3.0 and Windows Server 2012. 
  14. .LINK 
  15.     This script posted to: 
  16.         http://www.pshscripts.blogspot.com 
  17. .EXAMPLE 
  18.     Psh> Disable-GUI -Verbose 
  19.     Removing Windows Feature: Server-GUI-Shell, Server-Gui-Mgmt-Infra 
  20.     Setting Shell Registry setting to PowerShell 
  21.     Restarting the computer - please be patient 
  22.     < after reboot, full GUI is added > 
  23. #> 
  24.  
  25. Function Disable-GUI { 
  26.  
  27. # No parameters - but maybe later 
  28. # Turn on CmdletBinding to enable -Verbose 
  29.  
  30. [Cmdletbinding()] 
  31. Param() 
  32.  
  33. # Remove features from main Server core and downgrade GUI to Server Core 
  34. Write-Verbose "Removing Windows Feature: Server-GUI-Shell, Server-Gui-Mgmt-Infra" 
  35. Uninstall-WindowsFeature Server-Gui-Shell, Server-Gui-Mgmt-Infra 
  36.  
  37. # Add in PowerShell as the shell 
  38. Write-Verbose "Setting Shell Registry setting to PowerShell" 
  39. $RegPath = "Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\winlogon"  
  40. Set-ItemProperty -Path $RegPath -Name Shell -Value 'PowerShell.exe -noExit -Command "$psversiontable"'  -Force 
  41.  
  42. # Now restart the system 
  43. Write-Verbose "Restarting the computer - please be patient" 
  44. Restart-Computer 

No comments: