PoshCode Logo PowerShell Code Repository

Stop service and wait... by Process Service 4 months ago (modification of post by AdrianWoodrup view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6657"></script>download | new post

Very simple script that stops a service and waits for it to stop before rebooting. This script can be edited so it runs on every reboot or can be run manually to reboot.

  1. <#
  2. This script stops the service, then waits for the service to stop before continuing with the reboot/shutdown
  3. The scritp can be pushed to a server/Pc using Group Policy or Registry or run manually.
  4. The shutdown script Registry key is:
  5.         HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Shutdown\
  7. #>
  8. # type the name of the service in the quotes here
  9. $ServiceName = 'Service Name'
  11. Stop-Service $ServiceName
  12. write-host $ServiceName'...' -NoNewLine
  13. $TestService = Get-Service  $Service | Select-Object 'Status'
  14. While($TestService | where {$_.Status -eq 'Running'}){ 
  15.         Write-Host '.'-NoNewLine
  16.         Sleep 2
  17.         }
  19. # If you want to shutdown the computer add the command "Shutdown /t 0" (without quoutes)onto the bottom line.
  20. # If you want to Reboot the computer then add the command "Restart-computer" (without quotes) on the line below.

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

Remember me