Powershell script not working through Task Scheduler

Pankajbhakta95 96 Reputation points
2021-04-15T01:35:54.57+00:00

Hi,
I am trying to disable few users on AD with the help of a simple PowerShell script as below.


Import-Module ActiveDirectory

Read content from DisableUser.csv file

$users=Import-CSV C:\ADUSERS\DisableUsers.csv

ForEach ($user in $users)
{
Disable-ADAccount -Identity $($user.name)

}


When I run the above script from my laptop through PowerShell console it works fine and disables the AD accounts but the script does not run
through the Task Scheduler. In the Scheduler I found that the Task starts and finishes but the script does not run.

I even added the command below to check it the script runs but no luck.

Set-Content -Path "C:\ADUSERS\TestTaskSch.txt" -Value "Hello World"

The Task Scheduler settings are given below.

Program/Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Argument : -File "C:\PSScripts\DisableADUsers.ps1" ( file location of the script)

Kindly help.

Cheers

Pankaj

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,644 questions
{count} votes

Accepted answer
  1. MotoX80 31,581 Reputation points
    2021-04-15T23:53:56.263+00:00

    Look in the History tab for errors.

    Change the task to run cmd.exe instead of powershell.exe. In the arguments field specify a bat file name, C:\PSScripts\DisableADUsers.bat

    Create the .bat file to call the PS script but also redirect stdout and stderr to a log file where you can check for error messages.

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "C:\PSScripts\DisableADUsers.ps1"  1>C:\Windows\temp\DisableADUsers.log 2>&1 
    
    2 people found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. Pankajbhakta95 96 Reputation points
    2021-04-19T06:29:16.443+00:00

    Hi,

    I have tested the solution provided by @MotoX80 by creating a batch file and calling the PowerShell script from it.

    It is working fine.

    Later I read few more post and added the following.

    Argument -> - ExecutionPolicy Bypass
    Start in -> C:\PSScripts\

    The Task Scheduler config shown below.


    Program/Script -> C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

    Argument -> -ExecutionPolicy Bypass -File "C:\PSScripts\DisableADUsers.ps1"

    Start In -> C:\PSScripts\


    The earlier Scheduled Task is also working alright now.

    Hence, I can use either method.

    Thanks to both @Karlie Weng and @@MotoX80 for all your suggestion and help.

    Cheers

    Pankaj

    6 people found this answer helpful.

  2. Karlie Weng 14,181 Reputation points Microsoft Vendor
    2021-04-15T08:28:50.587+00:00

    Hello @Pankajbhakta95

    1. First please check your configuration steps :How to: Run PowerShell Scripts from Task Scheduler
    2. Change that to "Run whether user is logged on or not".
    3. Leave the Do Not Store password option unchecked
    4. Mark "Run with Highest Privileges" option
    5. The account being use to execute task must have "Logon as batch job" rights under the local security policy of the server (or be member of local Admin group). You must specified the account you need to run scripts/bat files.

    Please see:
    Task Security Context

    Powershell script does not run via Scheduled Tasks

    Best Regards
    Karlie

    ----------

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Pankajbhakta95 96 Reputation points
    2021-04-15T20:10:51.47+00:00

    @Karlie Weng

    Thanks for your reply.

    All the points you have mentioned in your reply are true but no luck.

    Please check the screenshot of Task Scheduler.

    88355-task-scheduler.jpg

    Cheers

    Pankaj