Windows – Scheduled Task for Powershell Script

powershellscriptingwindowswindows-server-2012

So I'm trying to run a powershell script on a schedule. Every morning at 6am, repeat every hour of the day. Here is the script itself:

Get-Content C:\Users\administrator\Desktop\users.txt | ForEach-Object {
    Set-AdUser -Identity $_ -LogOnWorkstations $null
}

Now, the script works flawlessly when I execute it myself. But it seems as if the script won't properly run when its being scheduled to execute. My thoughts is that the execution policy keeps asking if its safe to run the script prior to running it. If anyone else agrees that this might be the reason the script can't start on its own, could you please provide me a solution around this barrier?

Best Answer

Where is the sheduled task? On GPO or on a local machine? Are the script and the machine where it should run in the same network?

Try to schedule it like this (if you haven't done that yet):

Programm/Script: PowerShell.exe

Arguments: -ExecutionPolicy Bypass -Command "& 'FilePathToScript.ps1'"

Edit: Also I'm not quite sure if you have to have the AD-PS-Module installed on the Workstation where you want to run the script, because it could be that the command Set-ADUser cannot be found.