Stop service and wait

Posted On 2017-12-10 by awoodrup
Tags: Powershell Scripting Windows
Views: 73

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.

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

About the Author

awoodrup has posted a total of 1 articles.

Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Code Links