Scheduling a task with PowerShell 3

The three components needs to schedule a job in PowerShell are:

New-JobTrigger
New-ScheduledJobOption
Register-ScheduledJob
New-JobTrigger and New-ScheduledJobOption can be run and assigned to a variable so that the output of both commands can be used in Register-ScheduledJob

 

NAME
New-JobTrigger

SYNOPSIS
Creates a job trigger for a scheduled job
SYNTAX
New-JobTrigger [-Once] [-RandomDelay <TimeSpan>] [-RepeatIndefinitely] [-RepetitionDuration <TimeSpan>] [-RepetitionInterval <TimeSpan>] -At <DateTime> [<CommonParameters>]

New-JobTrigger [-Weekly] [-RandomDelay <TimeSpan>] [-WeeksInterval <Int32>] -At <DateTime> -DaysOfWeek <DayOfWeek[]> [<CommonParameters>]

New-JobTrigger [-Daily] [-DaysInterval <Int32>] [-RandomDelay <TimeSpan>] -At <DateTime> [<CommonParameters>]

New-JobTrigger [-AtLogOn] [-RandomDelay <TimeSpan>] [-User <String>] [<CommonParameters>]

New-JobTrigger [-AtStartup] [-RandomDelay <TimeSpan>] [<CommonParameters>]
DESCRIPTION
The New-JobTrigger cmdlet creates a “job trigger” that starts a scheduled job on a one-time or recurring schedule, or when an event occurs.
To make the trigger logon you could use:

$trigger = New-JobTrigger -AtLogOn

To create a scheduled option

NAME
New-ScheduledJobOption

SYNOPSIS
Creates an object that contains advanced options for a scheduled job.
SYNTAX
New-ScheduledJobOption [-ContinueIfGoingOnBattery] [-DoNotAllowDemandStart] [-HideInTaskScheduler] [-IdleDuration <TimeSpan>] [-IdleTimeout <TimeSpan>] [-MultipleInstancePolicy <TaskMultipleInstancePolicy>] [-RequireNetwork]
[-RestartOnIdleResume] [-RunElevated] [-StartIfIdle] [-StartIfOnBattery] [-StopIfGoingOffIdle] [-WakeToRun] [<CommonParameters>]
DESCRIPTION
The New-ScheduledJobOption cmdlet creates an object that contains advanced options for a scheduled job.

$joboption = New-ScheduledJobOption -RequireNetwork -WakeToRun

To actually schedule the job you need to run

Register-ScheduledJob

NAME
Register-ScheduledJob

SYNOPSIS
Creates a new scheduled job.
SYNTAX
Register-ScheduledJob [-Name] <String> [-ScriptBlock] <ScriptBlock> [-ArgumentList <Object[]>] [-Authentication <AuthenticationMechanism>] [-Credential <PSCredential>]     [-InitializationScript <ScriptBlock>] [-MaxResultCount <Int32>] [-RunAs32] [-RunNow]       [-ScheduledJobOption <ScheduledJobOptions>] [-Trigger <ScheduledJobTrigger[]>]
[-Confirm] [-WhatIf] [<CommonParameters>]

Register-ScheduledJob [-Name] <String> [-FilePath] <String> [-ArgumentList <Object[]>]  [-Authentication <AuthenticationMechanism>] [-Credential <PSCredential>]                      [-InitializationScript <ScriptBlock>] [-MaxResultCount <Int32>] [-RunAs32] [-RunNow]      [-ScheduledJobOption <ScheduledJobOptions>] [-Trigger <ScheduledJobTrigger[]>]
[-Confirm] [-WhatIf] [<CommonParameters>]
DESCRIPTION
The Register-ScheduledJob cmdlet creates scheduled jobs on the local computer.
So now I am going to schedule a job that run get-service at logon

Register-ScheduledJob -Name “view services at logon” -ScriptBlock { Get-Service } -Trigger $trigger -ScheduledJobOption $joboption

1

Just to prove this works I have logged off and back on again.

2