Schedule a Powershell script in Windows 8?

1

How can I schedule a Powershell script to run every day at 3am in Windows 8?

Wesley Tansey

Posted 2012-12-27T22:04:50.547

Reputation: 233

1What have you tried so far? Did you attempt to use Task Scheduler? Use powershell /? to know how to run a PS1 script from the command-line. – Karan – 2012-12-27T22:30:29.180

I tried using Task Scheduler to run the script, but when I try to test it by running it from the Scheduler, its status changes to Running and never finishes. Note that I tested the script and it successfully runs and exits if you right click it run it with Powershell. – Wesley Tansey – 2012-12-27T22:36:25.017

You should have mentioned this in the original question, else as you can see below people will just waste their time teaching you how to create a scheduled task. Did you try setting the task to run as admin/with highest privileges? – Karan – 2012-12-27T22:45:07.760

Yes, I tried that as well. It is set to run with highest privileges. – Wesley Tansey – 2012-12-27T22:48:34.897

Did you try on another Win8 PC, if you have access to one? I think it might be difficult to diagnose this issue unless the script contents are known. What are you trying to do with PS? – Karan – 2012-12-27T22:54:27.230

I don't have access to another PC. The script just copies a file via Copy-Item. I guess what I'm really asking is: what should I point Task Scheduler to? The Script? Powershell -file Path\To\Script? Something else? – Wesley Tansey – 2012-12-27T23:04:11.153

How about to powershell.exe, and specify the script as a parameter? That's what I was referring to when I mentioned powershell /? above. – Karan – 2012-12-27T23:08:23.057

Yep. Specifically, I set the program to C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe and the arguments to -File C:\Users\Username\Desktop\backupapp\backup.ps1. – Wesley Tansey – 2012-12-27T23:14:25.940

I noticed after a reboot that when I try to run it by right-clicking on it, Powershell is now asking me to confirm that I want to change the execution policy. I suppose it's possible that the script is blocking (waiting for input) in the background when the scheduler runs. If that's the case, I need some way to make it not ask for confirmation of the execution policy change at start. – Wesley Tansey – 2012-12-27T23:19:11.533

Also, I'll add that the command works from the command line just fine. – Wesley Tansey – 2012-12-27T23:23:22.410

At the PS prompt, type this command: set-executionpolicy remotesigned Now you can run local unsigned scripts. – Karan – 2012-12-27T23:24:18.460

Right. I had done that before-- otherwise it would be Restricted by default and not allow any scripts to run from the command line or anywhere. I am saying it asked for the confirmation to switch to RemoteSigned again after a reboot. – Wesley Tansey – 2012-12-27T23:26:14.443

Weird. Ok, how about scheduling a batch file that sets the execution policy, then runs the script? – Karan – 2012-12-27T23:27:24.897

Hmm.. I may be misunderstanding how Task Scheduler works. I enabled task history and it is successfully completing the task and the file is copied. However, it never changes from the "Running" to "Ready" state. Am I just expecting something that won't happen if I force-run rather than letting a trigger happen? – Wesley Tansey – 2012-12-27T23:34:48.247

1Why not set a trigger a few mins. from now and simply test? – Karan – 2012-12-27T23:37:01.830

Looks like it works fine if I use a trigger. Thanks Karan! – Wesley Tansey – 2012-12-27T23:51:53.120

If you want pure PowerShell3 solution I recommend reading Scheduled Jobs CMDlets.

– Darius – 2013-01-03T01:22:51.193

Answers

2

EDIT: This is a proof of concept that worked for me.

  1. open a new textfile and type in
    write-Host 'Hello World'
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

  2. save it as hello.ps1 on your desktop

  3. open the 32-bit version of powershell.exe with elevated rights
    (right click + open as administrator)
    C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe

  4. make sure, that you didn't use the x64 version until you want to
    C:\Windows\syswow64\Windowspowershell\v1.0\powershell.exe

  5. On my Windows 7 the "Windows PowerShell (x86)" startmenu shortcut points to the x64 version! These versions use seperate policy settings. It took me a while to figure that out :)

  6. Type in the following to allow powershell scripts and confirm it with Y
    Set-ExecutionPolicy Unrestricted

  7. That setting is stored permanently. Do a reboot just to check that.

  8. check the powershell policy and confirm that it is still unrestricted with
    get-ExecutionPolicy

  9. open schedule task window

  10. make a scheduled task which points at the x86 version
    C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe

  11. Type in the following for the optional parameter:
    c:\users\YOURUSERNAME\desktop\hello.ps1

  12. Execute your task manually through a right-click in your schedule task window

  13. Wonder why it says "running"

  14. Hit F5 to refresh. You will notice that the "still running"-status disappears.

  15. Be happy (after 1 hour :p)

nixda

Posted 2012-12-27T22:04:50.547

Reputation: 23 233

Step 14 was very helpful to me, I think a lot of people have issues about their tasks never finishing. Thank you! – Tim – 2015-09-09T15:23:09.740

That does not work. Please see my comment in the original post. – Wesley Tansey – 2012-12-27T22:36:42.193

What was the exact syntax you used in your task scheduler? – nixda – 2012-12-27T23:08:09.297

I tried it the way you suggested in your edit. It still just changes the status to Running and never finishes. – Wesley Tansey – 2012-12-27T23:17:05.290

Please see my full edit. There has to be a normal way. – nixda – 2012-12-28T01:04:51.313

Regarding step 6, it should probably say "confirm it with Y" (instead of J). I realise it's J for you because your Windows display language is set to German, but SU normally assumes English localisation, unless specified otherwise. – Indrek – 2013-01-02T23:56:04.287

0

run 'taskschd.msc', navigate to Powershell and create the job/schedule.

Reference

Wes

Posted 2012-12-27T22:04:50.547

Reputation: 539

0

It took me a while to get my Scheduled PowerShell task to work.

Here are a few tactics that helped me.

a) Get a simple PowerShell script working.
b) Understand all the various Task Manager tabs, including the conditions and actions (I think you have done this already)
c) Watch the path to the various files.


My killer problem was not realising PowerShell was the program and the script was merely an argument.

Another minor help was introducing -ExecutionPolicy Bypass before the file name thus:
-ExecutionPolicy Bypass -File "D:\PShell\Process Task.ps1"

Finally, I am never sure about the SuperUser etiquette, but this is my page on the subject of Scheduling PowerShell.

Guy Thomas

Posted 2012-12-27T22:04:50.547

Reputation: 3 160