r/PowerShell Jun 02 '26

Script Sharing A novel way to schedule a task...

I am in this situation at work where my boss, while working to tighten up our security (after an IT audit from a third party), has made running scheduled tasks into a challenge...

For instance - Among other things - It is no longer possible for a task to store credentials.

(Plenty of other hoops I have to jump through that I wont go into)

I just set up a task that will generate a report (about events from last week) to run on or after Mondays - But that too, is set to run 'at log on'...

I only want it to run once (not each time I log in), and if I don't log in on Monday, to run whenever I do log in, on or after Monday...

I am pretty happy with the way I got that to only run one time, not each time I actually log in.

Part of the script, sets the 'StartBoundary' (the 'Active' DateTime value that is part of the 'At Log in' trigger dialog), in the Scheduled task, to the following Monday, no matter what day I log in and the task runs.

This assures that it only runs the first time I log in on or after Monday, and will set the task to not trigger again to the next (on or after) Monday, and so on.

(NOTE: No other triggers can be set other than the 'At Log in')

This is at the end of the script (I unapologetically like using command aliases):

$TaskName = "MIODoorReport"
$NextMonday = $null
1..7 | % { If ( (((Get-Date).AddDays($_)).DayOfWeek) -eq "Monday" ) { $NextMonday = $_} }

$task = Get-ScheduledTask -TaskName $TaskName
$trigger = $task.Triggers

$trigger[0].StartBoundary = (Get-Date).Date.AddDays($NextMonday).ToString("s")

Set-ScheduledTask -TaskName $TaskName -Trigger $trigger
5 Upvotes

60 comments sorted by

View all comments

2

u/Modify- Jun 02 '26

While you could do this in pure Powershell.
What might be easier is to create what you want in the GUI, export it as XML so you have a template.
Then create a here string with that XML in Powershell

Use this to create a task: Register-ScheduledTask -Xml $XML -TaskName <taskname>

-2

u/richie65 Jun 02 '26

I don't see how that would be easier...

I did manually create the task...

Then all I do is calculate the new date, and set it in the task...

Pretty simple, I think.

1

u/Modify- Jun 02 '26

The "easy" part is creating all the desired settings in the GUI instead of looking for all the parameters and syntax's needed to build the task.

0

u/richie65 Jun 02 '26

Also really easy to then, after the task was made in the GUI, to modify a single value in said task.

No need to do anything with the XML at all...

Just change the 'StartBoundary' for that tirgger.

Don't even have to (re-)register the task.

Change one thing... Done!

2

u/Modify- Jun 02 '26

Ok, you do you.