In this example I am creating a task to restart a service without first creating and calling a batch script (.bat, .cmd) using a powershell one-liner. For example
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "Restart-Service -Name 'Dhcp'"
rem Replace Dhcp with whichever service name you want to restart
If creating a service restart task. Remember to check the "Run with the highest privileges" option
Get the path to powershell.exe
In a powershell terminal find powershell home to find full path to powershell by entering $PSHome
Append powershell.exe to get the full path for powershell
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Create an action in a Task Scheduler Task
In the actions tab of Task Scheduler click New...
Enter the path to powershell (i.e. C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
) as the Program/script and then in the "Add arguments (optional):" field enter the powershell command you want to run. For example this command restarts a service named 'Your Service Name Here'
-command "Restart-Service -Name 'Your Service Name Here'"
Test the command line first
It is a good idea to test the command line before attempting to put it in a task scheduler action. If you see errors output then you have to fix your command line until it works correctly.
Test your scheduled task
Once you have created your scheduled task manually run it by right clicking on the task name and choosing Run from the context menu
After it has finished running (you might need to refresh the task list by pressing F5 because it can say "Running" after the task has completed) look for an exit status of (0x0) generally anything other than (0x0) indicates an error.
Getting a service name if you are wanting to restart-services as per above example
To get the name of any service in windows open services.msc
and right click on a service and choose properties the name you want is the Service name: field. Typically service names do NOT have spaces but sometimes they do so you might need to wrap the name in single quotes as above.
0 Comments