Automating repetitive tasks is essential for many IT professionals and system administrators. One of the most useful tools for automating tasks on a Windows machine is PowerShell. However, scheduling PowerShell scripts to run at specific times can be a bit of a challenge. Fortunately, there is a solution: the New-OHScheduledTask function.

What is New-OHScheduledTask?

The New-OHScheduledTask function is a PowerShell cmdlet that creates a new scheduled task. It is a powerful tool that can help you automate a wide range of tasks, from running PowerShell scripts to launching applications and running batch files.

How to Use New-OHScheduledTask

To create a new scheduled task using the New-OHScheduledTask function, you first need to specify the task’s properties, such as the task name, the program or script to run, and the schedule for running the task. The function has been optimised to quickly schedule PowerShell scripts although it can just as easily schedule an exe. As I use it in my workplace primarily for Powershell, I’ll focus the examples on that:

Example 1: Deploying a simple PowerShell script

New-OHScheduledTask -TaskName "MyTask" -TaskDescription "Runs a simple PowerShell script." -Trigger AtStartup -AllowedUser 'NT AUTHORITY\SYSTEM' -ScriptPath "c:\ohtemp\test2.ps1" -Action replace -RunWithHighestPrivilege

This example demonstrates how to create a new scheduled task named “MyTask” that runs a PowerShell script at system startup. The task is configured to replace any existing task with the same name, and it will run with the highest privileges.

To quickly schedule the script, the -ScriptPath parameter is utilized, which allows you to supply the path to the .ps1 file. This parameter provides a simple and efficient method for scheduling PowerShell scripts.

Example 2: Deploying a complex PowerShell script

New-OHScheduledTask -TaskName "MyTask" -TaskDescription "Runs a PowerShell script with lots of arguments." -Trigger AtStartup -AllowedUser 'NT AUTHORITY\SYSTEM' -Program "C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe" -Arguments '-noprofile -executionpolicy bypass -command "& { . c:\ohtemp\test2.ps1; Show-Text -textToDisplay "Important Text!" }' -Action Add -RunWithHighestPrivilege

This example demonstrates how to create a new scheduled task named “MyTask” that runs a PowerShell script with multiple arguments at system startup. To ensure that the task is added only if the task name does not already exist, it is configured with the -Action Add parameter. Additionally, the task is set to run with the highest privileges.

In the PowerShell script used in this example, the “Show-Text” function is utilized to display the message “Important Text!”. This function is contained within the script Test2.ps1 and can be executed using the -Program parameter.

Using the -Program parameter allows you to schedule executables and is particularly useful for scheduling more complex PowerShell scripts like the one used in this example, where a function with parameters is executed from the script.

Example 3: Deleting a Scheduled Task

New-OHScheduledTask -TaskName "MyTask" -Delete

This example deletes the task named, “MyTask” if it exists.

Parameters

New-OHScheduledTask is highly cusomisable and has the following parameters:

  • TaskName: The name of the scheduled task.
  • TaskDescription: A description of the scheduled task.
  • Trigger: The trigger for the scheduled task. The accepted values are AtLogon and AtStartup.
  • AllowedUser: The user account that is allowed to run the scheduled task. The accepted values are BUILTIN\Users and NT AUTHORITY\SYSTEM.
  • ScriptPath: The path to the PowerShell script that the scheduled task runs. This parameter is mandatory if the parameter set name is Script.
  • Program: The path to the program that the scheduled task runs. This parameter is mandatory if the parameter set name is Program.
  • Arguments: Specifies the arguments that the program uses. This parameter is optional.
  • StartIn: Specifies the starting directory for the program. This parameter is optional.
  • Action: Specifies the action for the scheduled task. The accepted values are add and replace.
  • DelayTask: Specifies the delay time for the scheduled task. The accepted values are 30s, 1m, 30m, and 1h. This parameter is optional.
  • TaskFolder: Specifies the folder for the scheduled task. The default value is OHTesting.
  • RunWithHighestPrivilege: Runs the scheduled task with the highest privileges. This parameter is optional.
  • StartTaskImmediately: Starts the scheduled task immediately. This parameter is optional.
  • Delete: Deletes the scheduled task. This parameter is optional.

Conclusion

In conclusion, the New-OHScheduledTask function is a powerful tool that can help you automate a wide range of tasks on your Windows machine. Whether you need to run a PowerShell script, launch an application, or execute a batch file, this function can help you get the job done. The function’s customizable parameters allows you to create scheduled tasks quickly and efficiently and you can find it in my GitHub repo: https://github.com/ozthe2/MyPowerShell/blob/main/New-OHScheduledTask.ps1.


“Understanding Microsoft Intune: Deploying Applications Using PowerShell” is available for purchase at all good book stores and online outlets. Don’t miss out on the opportunity to take your application deployment skills to the next level. Get your copy today!

Amazon.co.uk

Amazon.com

Apress