Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / PowerShell / Run PowerShell Scripts on a Schedule with Task Scheduler

October 3, 2024

Run PowerShell Scripts on a Schedule with Task Scheduler

In Windows, the built-in Task Scheduler can be used to perform an action according to a schedule or when a certain event occurs. This guide explains how to configure a PowerShell script to run automatically by using the Windows Task Scheduler. The PS1 script should run in the background, display no pop-ups, and run regardless of the current PowerShell script execution policy settings.

In this example, I want to run the C:\PS\Outlook_Email_to.ps1 PowerShell script file every 10 minutes.

  1. Open the Task Scheduler console by running taskschd.msc command
  2. Expand the Task Scheduler library tree. For convenience, create a separate folder for your custom scheduled tasks. Right-click and select Create Task.
    Create a new task using Windows Task Scheduler
  3. In the General tab, specify the task name and the user it will run under. The task can run automatically:
    – when the specific user is logged in (Run only the task is logged in)
    – or whether the user is logged in or not (Run whether user is logged on or not).

    The second mode is used most often. In the second case, you can specify that the task should run on behalf of a specific user (the Credentials Manager used to store the user’s password). If the task requires elevation, enable the ‘Run with highest privileges‘ option. Schedule task to run when user is logged on or not

    To avoid using a stored password, you can configure the Task to run as NT AUTHORITY\SYSTEM with the highest privileges. For that, enter SYSTEM in the User field. Scheduling a task with high (system) privilege
    In an AD environment, the scheduled tasks can run on behalf of the gMSA managed service accounts.
  4. In the Triggers tab, specify the condition or time for the Scheduler task to start. For example, to run a task when a user logs in, select the ‘At log on‘ trigger and select a frequency of 10 minutes in the ‘Repeat task every‘ option. Run PS1 script at logon with Task Scheduler
  5. If the task runs on behalf of SYSTEM or a user with a stored password, select to run the task when Windows starts (At startup) and to restart it periodically.
    Run task at startup
  6. Or use the On a schedule trigger to set the exact time for the task to start. Multiple start triggers can be configured for a single task.
    The scheduler can also run a task when a specific event occurs in the Event Viewer (see How to run a scheduled task after another task has finished).
  7. Then go to the Actions tab. Specify the action to be taken when any of the triggered events occur. I want to run a PowerShell script in this case. Select New -> Start a program. Configure the following action settings:
    Program/script: powershell.exe
    Add arguments (optional): -ExecutionPolicy Bypass -NonInteractive -WindowStyle Hidden -File "C:\PS\Outlook_Email_to.ps1" Running a Powershell script from Task Scheduler
    Before running the script through the Task Scheduler, check that it returns no errors in unattended mode. Use the following command:
    powershell.exe -file C:\PS\ Outlook_Email_to.ps1 -NoExit
  8. The following options are used to run a PowerShell script:
    -File – full path to the script file (PS1)
    -ExecutionPolicy — Set PowerShell script execution policy settings for the current session. Current policy settings are ignored and the script is executed anyway if Bypass is specified;
    -NonInteractive – Do not display interactive prompts to the user
    -WindowStyle Hidden – Hide the PowerShell console window from the user (the script runs hidden). The PowerShell prompt window may appear and disappear momentarily while the script is running if the scheduler task is set to run when the user logs on. There is no flashing prompt only for scripts started in console session 0 (regardless of user login).
    -NoProfile — add this option if the script can work without a user profile. It prevents the user profile from being loaded, which speeds up the execution of the script;
  9. You can enable the following useful options in the Settings tab:
    Allow task to be run on demand
    If the running task does not end when requested, force it to stop
    Do not start a new instance
  10. Save the task settings. Check that the new task appears in the Task Scheduler snap-in. Click on a task and select Run to test it.
    Task Scheduler Last Run Result code 0x0
    If the PowerShell script has been run successfully, a message will be displayed in the Last Run Result:

    The operation completed sucessfully (0x0).

    To log all actions to a text log file, we recommend that you add a simple logging function to the PowerShell script. This allows viewing detailed information on all actions performed at any time.
  11. Use the History tab to view the history and results of previous Task runs. Task History is not saved by default in Task Scheduler (click the Enable All Tasks History link in the Actions pane).
    View history of scheduled tasks

You can also create such a Scheduler task to run a PowerShell script from a command prompt:

$TaskName="CheckOutlookMailbox"
$Trigger = New-ScheduledTaskTrigger -AtStartup
$Trigger.Repetition = (New-ScheduledTaskTrigger -once -at "12am" -RepetitionInterval (New-TimeSpan -Minutes 10) -RepetitionDuration (New-TimeSpan -Minutes 10)).repetition
$User= "NT AUTHORITY\SYSTEM"
$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-ExecutionPolicy Bypass -NonInteractive -WindowStyle Hidden -File C:\PS\Outlook_Email_to.ps1"
Register-ScheduledTask -TaskName $TaskName -Trigger $Trigger -User $User -Action $Action -RunLevel Highest -Force

There are some additional things to consider when running PowerShell scripts through the Windows Task Scheduler:

  • To run the script in the PowerShell Core environment, run pwsh.exe instead of powershell.exe.
  • If other users have access to the computer on which you are running the PowerShell script with privileged rights, make sure that you have changed the NTFS access permissions on the PS1 file so that they cannot modify it.
  • If the task is run as an unprivileged user, their account must be added to the local security policy Log on as a batch job (gpedit.msc -> Computer Configuration -> Windows Settings -> Security Settings -> Local Policies -> User Rights Assignment). A warning will appear when creating such a task:
    This task requires that the user account specified has Log on as batch job rights

    This task requires that the user account specified has Log on as batch job rights

  • In an AD domain, you can use the GPO to run PowerShell scripts when a user logs on or off, or when a computer starts or shuts down. Such scripts are known as logon scripts.
4 comments
2
Facebook Twitter Google + Pinterest
PowerShellWindows 11Windows Server 2022
previous post
Graylog: Centralized Log Collection and Analysis
next post
Validating AD User Credentials with PowerShell

Related Reading

How to Assign (Passthrough) a Physical GPU to...

June 11, 2024

Extend an Expired User Password in Active Directory

December 23, 2024

Tracking Printer Usage with Windows Event Viewer Logs

March 12, 2024

Adding ESXi Host to VMware vCenter Server (vCSA)

March 12, 2024

PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

March 17, 2024

How to Add or Remove Pinned Folders to...

August 11, 2024

Check the Software Installation/Removal History in Windows

October 8, 2024

Configure File and Folder Access Auditing on Windows...

July 8, 2024

4 comments

serg October 4, 2024 - 9:26 am

To run your PowerShell script without UAC elevation prompt, check the “Run with highest privileges” option in the task settings.

Reply
sajid November 2, 2024 - 5:03 pm

i am looking to run some script remotely to disable NetBIOS over tcp on latest windows 11 22h2 but fail because the scripte needs admin access. if you can give some examples.
this
set-ItemProperty HKLM:\SYSTEM\CurrentControlSet\services\NetBT\Parameters\Interfaces\tcpip* -Name NetbiosOptions -Value 2
or this
Invoke-CimMethod -Query ‘SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=1’ -MethodName SetTcpipNetbios -Arguments @{TcpipNetbiosOptions=[uint32]2}

Reply
admin November 11, 2024 - 6:32 am

You cannot change these settings without administrator permissions. There can be some nuances when using administrator credentials over the network in workgroup and AD environments. Which one do you use?

Reply
sajid November 12, 2024 - 8:02 am

i am using domain admin cred. (let me do it again and see)

Reply

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Recent Posts

  • Map a Network Drive over SSH (SSHFS) in Windows

    May 13, 2025
  • Configure NTP Time Source for Active Directory Domain

    May 6, 2025
  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • How to Delete Old User Profiles in Windows
  • Fix: Remote Desktop Licensing Mode is not Configured
  • How to Install Remote Server Administration Tools (RSAT) on Windows
  • How to Create UEFI Bootable USB Drive to Install Windows
  • Configuring User Profile Disks (UPD) on Windows Server RDS
  • Wi-Fi (Internet) Disconnects After Sleep or Hibernation on Windows 10/11
  • How to Allow Non-Admin User to Start/Stop Service in Windows
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top