PowerShell or batch script for restart service on remote PC

Guys I need some help with PowerShell or batch script which I am not very good in.

I want to stop/kill, delete any temp file and then start a service on remote machine.

Machine name is Leo10
Service name is Leoadmin.exe
Some time I restart service but it doesn’t stop so I kill the process then start and delete some temp folders.

Here is the process which I want in script.

On Leo10 server
Stop Leoadmin.exe
If failed then kill process Leoadmin.exe
Delete file on Leo10 server desktop/temp folder if any
Start Leoadmin.exe service

Thank you
Who is Participating?
d_nedelchevConnect With a Mentor Commented:
sc query leoadmin

Open in new window

to see if the servce is STOPPABLE at all.
xDUCKxConnect With a Mentor Commented:
net stop leoadmin
taskkill /F /IM leoadmin.exe
del C:\User\Destkop\Temp\*.tmp
net start leoadmin

Open in new window

The "Net Stop" and "Net start" may or may not work depending on what the actual name of the service is displayed as in services.msc.

Test it before putting it into production.

EDIT:  This is a batch file.
David Johnson, CD, MVPOwnerCommented:

stop-Service -InputObject $(Get-Service -Computer LEO10-Name leoadmin
#give it a few seconds to stop
Start-Sleep -s 5
Stop-Process -InputObject $(Get-Service -Computer LEO10 -Name leoadmin.exe
start-sleep -s 2
start-service -InputObject $(Get-Service -Computer LEO10 -Name leoadmin
VijaytyagiAuthor Commented:

Thanks for your advice but both way (PS and batch script) not able to stop service.

Killing process is working both way.

Any other suggestion?

Thank you
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.