create monthly scheduled task

Hi Guys,

I created some scheduled tasks and they run some .ps1 scripts which helps allot in some daily tasks to automate.

I would like also to create some scheduled tasks which run each month on the first.

I only don’t see a monthly option, only Once, Daily, Weekly, AtLogon, AtStartup.

Below the script i use right now, could somebody help me?

# Script
$Action = New-ScheduledTaskAction -Execute C:\Scripts\script.cmd
# Trigger
$Trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 01:00am
# Account and logon 
$Principal = New-ScheduledTaskPrincipal -UserId domain\mangedserviceaccount -LogonType Password -RunLevel Highest
# Create task
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName "Taskname"  -Principal $Principal -Force

You could use schtasks to create a monthly task. Look at the help with

schtasks /create /?

You can program the task scheduler manually. Example:

# https://docs.microsoft.com/en-us/windows/desktop/taskschd/task-scheduler-start-page
# https://docs.microsoft.com/en-us/windows/desktop/taskschd/taskservice

$scheduler = New-Object -ComObject('Schedule.Service')
$scheduler.Connect()

$root = $scheduler.GetFolder('\')

$task = $scheduler.NewTask(0) # flags, reserved, must be 0

$task.RegistrationInfo.Description = 'do this first monday each month'
$task.RegistrationInfo.Author = 'James Bond'
$task.settings.StartWhenAvailable = $true

# https://docs.microsoft.com/en-us/windows/desktop/api/taskschd/ne-taskschd-_task_trigger_type2
$trigger = $task.Triggers.Create(5) # 5=TASK_TRIGGER_MONTHLYDOW
$trigger.StartBoundary = [datetime]::new(2018, 1, 1, 1, 0, 0).ToString("yyyy-MM-dd'T'HH:mm:ss") # year, month, day, hour=1am, minute, second
$trigger.DaysOfWeek = 2 # monday

<#
https://docs.microsoft.com/en-us/windows/desktop/taskschd/monthlydowtrigger-monthsofyear
January 	0X01 	1
February 	0x02 	2
March 	0X04 	4
April 	0X08 	8
May 	0X10 	16
June 	0X20 	32
July 	0x40 	64
August 	0X80 	128
September 	0X100 	256
October 	0X200 	512
November 	0X400 	1024
December 	0X800 	2048
#>

$trigger.MonthsOfYear =
    0x1 -bor
    0x2 -bor
    0x4 -bor
    0x8 -bor
    0x10 -bor
    0x20 -bor
    0x40 -bor
    0x80 -bor
    0x100 -bor
    0x200 -bor
    0x400 -bor
    0x800

$action = $task.Actions.Create(0) # 0=TASK_ACTION_EXEC (command-line operation)
$action.Path = 'C:\Scripts\script.cmd'
$action.Arguments = ''

$task.Principal.RunLevel = 1 # TASK_RUNLEVEL_HIGHEST
$task.Principal.LogonType = 1 # TASK_LOGON_PASSWORD
$task.Principal.UserId = 'domain\mangedserviceaccount' # NT AUTHORITY\SYSTEM

# https://docs.microsoft.com/en-us/windows/desktop/taskschd/taskfolder-registertaskdefinition
$newtask = $root.RegisterTaskDefinition('Taskname', $task, 6, $null, $null, 1)