PowerShell script to restart a service

Question:

My mission is to press a keyboard sequence, such as Ctrl +Shift +R, to restart a Windows Service.

I have a script which works fine in the PowerShell ISE, when launched with administrative privileges.

When I try with a PowerShell script it fails due to insufficient Administrative Privileges. It’s galling that I can get it to work with an old-fashioned bat file, but not PowerShell.

The root of the problem is that shortcuts to a PowerShell script have their Administrative privileges box greyed out. So far no work-around has overcome this privilege problem.

Any ideas?

Answer:

One approach is to start another elevated PowerShell session within your script like so:

That should prompt to elevate the new PowerShell session. I think you should be able to set the -WindowStyle parameter such that the new window doens’t appear (if you need that behavior). Note that you will need to specify the full path to your existing script.

Source:

PowerShell script to restart a service by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply