Main example
List instances
# All instances of Win32_ScheduledJob, with the main properties
Get-CimInstance -ClassName Win32_ScheduledJob |
Select-Object -Property JobId, Name, Caption, Description, Status, InstallDateExamples
Filter with WQL
# WQL filter evaluated by the provider: DaysOfMonth = 1 (1)
Get-CimInstance -ClassName Win32_ScheduledJob -Filter 'DaysOfMonth = 1' |
Select-Object -Property JobId, Name, Caption, Description, Status, InstallDateThe filter is evaluated by the WMI provider: less data is transferred than with Where-Object.
Decode numeric values
# Lookup table for DaysOfMonth (values documented by Microsoft)
$daysOfMonthName = @{
1 = '1'
2 = '2'
4 = '3'
8 = '4'
16 = '5'
32 = '6'
64 = '7'
128 = '8'
256 = '9'
512 = '10'
1024 = '11'
2048 = '12'
4096 = '13'
8192 = '14'
16384 = '15'
32768 = '16'
65536 = '17'
131072 = '18'
262144 = '19'
524288 = '20'
1048576 = '21'
2097152 = '22'
4194304 = '23'
8388608 = '24'
16777216 = '25'
33554432 = '26'
67108864 = '27'
134217728 = '28'
268435456 = '29'
536870912 = '30'
1073741824 = '31'
}
# Lookup table for DaysOfWeek (values documented by Microsoft)
$daysOfWeekName = @{
1 = 'Monday'
2 = 'Tuesday'
4 = 'Wednesday'
8 = 'Thursday'
16 = 'Friday'
32 = 'Saturday'
64 = 'Sunday'
}
Get-CimInstance -ClassName Win32_ScheduledJob |
Select-Object -Property @(
'JobId'
'Name'
'Caption'
'Description'
'Status'
'InstallDate'
@{ Name = 'DaysOfMonth'; Expression = { $daysOfMonthName[[int]$_.DaysOfMonth] } }
)Query a remote computer (CIM session)
review before running# Name of the remote computer
$computerName = 'SERVER01'
# The CIM session (WSMan by default) can be reused for several queries
$cimSession = New-CimSession -ComputerName $computerName
try {
Get-CimInstance -CimSession $cimSession -ClassName Win32_ScheduledJob |
Select-Object -Property JobId, Name, Caption, Description, Status, InstallDate
} finally {
Remove-CimSession -CimSession $cimSession
}WSMan (WinRM) is used by default. For a host without WinRM, create the session with -SessionOption (New-CimSessionOption -Protocol Dcom). Add -Credential to use another account.
Show the class structure
# Class structure: properties, types and flags (Key, ReadOnly…)
$cimClass = Get-CimClass -ClassName Win32_ScheduledJob
$cimClass.CimClassProperties |
Select-Object -Property Name, CimType, Flags
# Methods of the class
$cimClass.CimClassMethods |
Select-Object -Property Name, ReturnTypeProperties
| Name | Type | Access | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Caption# | string | read | A short textual description of the object. Code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Command# | string | read | Name of the command, batch program, or binary file (and command-line arguments) that the schedule service uses to invoke the job. Example: "defrag/q/f" Code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DaysOfMonth# | uint32 | read | Days of the month when the job is scheduled to run. If a job is scheduled to run on multiple days of the month, these values can be joined in a logical OR. For example, if a job is to run on the 1st and 16th of each month, the value of the DaysOfMonth property would be 1 OR 32768. Values (31)
Code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DaysOfWeek# | uint32 | read | Days of the week when a job is scheduled to run. If a job is scheduled to run on multiple days of the week, the values can be joined in a logical OR. For example, if a job is scheduled to run on Mondays, Wednesdays, and Fridays the value of the DaysOfWeek property would be 1 OR 4 OR 16. Values (7)
Code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Description# | string | read | A textual description of the object. Code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ElapsedTime# | datetime | read | Length of time the job has been executing. Code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| InstallDate# | datetime | read | Indicates when the object was installed. Lack of a value does not indicate that the object is not installed. Code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| InteractWithDesktop# | boolean | read | Specified job is interactive, which means that a user can give input to a scheduled job while it is executing. Code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| JobId# key | uint32 | read | Identifying number of the job. It is used by methods as a handle to one job being scheduled on this computer. Code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| JobStatus# | string | read | Status of execution the last time this job was scheduled to run. Values (2)
Code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Name# | string | read | Label by which the object is known. When subclassed, this property can be overridden to be a key property. Code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Notify# | string | read | User is notified upon job completion or failure. Code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Owner# | string | read | User who submitted the job. Code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Priority# | uint32 | read | Importance of a job's execution. Code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RunRepeatedly# | boolean | read | Scheduled job runs repeatedly on the days that the job is scheduled. If False, then the job is run one time. Code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| StartTime# | datetime | read | UTC time to run the job, in the form of "YYYYMMDDHHMMSS.MMMMMM(+-)OOO", where "YYYYMMDD" must be replaced by "". The replacement is necessary because the scheduling service only allows jobs to be configured to run one time, or run on a day of the month or week. A job cannot be run on a specific date. The "(+-)OOO" section of the StartTime property value is the current bias for local time translation. The bias is the difference between the UTC time and local time. To calculate the bias for your time zone, multiply the number of hours that your time zone is ahead or behind Greenwich Mean Time (GMT) by 60 (use a positive number for the number of hours if your time zone is ahead of GMT and a negative number if your time zone is behind GMT). Add an additional 60 to your calculation if your time zone is using daylight savings time. For example, the Pacific Standard Time zone is eight hours behind GMT, therefore the bias is equals to -420 (-8 * 60 + 60) when daylight savings time is in use and -480 (-8 * 60) when daylight savings time is not in use. You can also determine the value of the bias by querying the bias property of the Win32_TimeZone class. For example: "123000.000000-420" specifies 14.30 (2:30 P.M.) PST with daylight savings time in effect. Code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Status# | string | read | String that indicates the current status of the object. Operational and non-operational status can be defined. Operational status can include "OK", "Degraded", and "Pred Fail". "Pred Fail" indicates that an element is functioning properly, but is predicting a failure (for example, a SMART-enabled hard disk drive). Non-operational status can include "Error", "Starting", "Stopping", and "Service". "Service" can apply during disk mirror-resilvering, reloading a user permissions list, or other administrative work. Not all such work is online, but the managed element is neither "OK" nor in one of the other states. Values include the following: Values (12)
Code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| TimeSubmitted# | datetime | read | Time that the job was submitted. Code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| UntilTime# | datetime | read | Time at which the job is invalid or should be stopped. Code |
Methods
Create static#
Class method that submits a job to the operating system for execution at a specified future time and date.
uint32 Create(
[in] string Command,
[in] datetime StartTime,
[in] boolean RunRepeatedly,
[in] uint32 DaysOfWeek,
[in] uint32 DaysOfMonth,
[in] boolean InteractWithDesktop,
[out] uint32 JobId
);Parameters
| Parameter | Type | Direction | Description |
|---|---|---|---|
| Command | string | in | Name of the command, batch program, or binary file and command-line parameters that the schedule service uses to invoke the job. Example: "defrag /q /f". |
| StartTime | datetime | in | Coordinated Universal Time (UTC) time to run a job. The form must be: "YYYYMMDDHHMMSS.MMMMMM(+-)OOO", where "YYYYMMDD" must be replaced by "". For example: "143000.000000-420" specifies 14.30 (2:30 P.M.) PST with daylight savings time in effect. The "(+-)OOO" section of the StartTime property value is the current bias for local time translation. The bias is the difference between the UTC time and the local time. To calculate the bias for your time zone, multiply the number of hours that your time zone is ahead or behind Greenwich Mean Time (GMT) by 60 (use a positive number for the number of hours if your time zone is ahead of GMT and a negative number if your time zone is behind GMT). Add an additional 60 to your calculation if your time zone is using daylight savings time. For example, the Pacific Standard Time zone is eight hours behind GMT, therefore the bias is equals to -420 (-8 * 60 + 60) when daylight savings time is in use and -480 (-8 * 60) when daylight savings time is not in use. You can also determine the value of the bias by querying the bias property of the Win32_TimeZone class. |
| RunRepeatedly | boolean | in | If True, a scheduled job runs repeatedly on specific days. The default is False. |
| DaysOfWeek | uint32 | in | Days of the week when a job is scheduled to run; used only when the *RunRepeatedly* parameter is True. To schedule a job for more than one day of the week, join the appropriate values in a logical OR. For example, to schedule a job for Tuesdays and Fridays, the value of *DaysOfWeek* are 2 OR 16. |
| DaysOfMonth | uint32 | in | Days of the month when a job is scheduled to run; used only when the *RunRepeatedly* parameter is True. |
| InteractWithDesktop | boolean | in | If True, the specified job should be interactive, which means that a user can give input to a scheduled job while the job is executing. The default is False. |
| JobId | uint32 | out | Identifier number of a job. This parameter is a handle to a job being scheduled on a computer. |
Return values
| Value | Label | Description |
|---|---|---|
| 0 | Successful completion | The request is accepted. |
| 1 | Not supported | The request is not supported. |
| 2 | Access denied | The user does not have the necessary access. |
| 8 | Unknown failure | Interactive process. |
| 9 | Path not found | The directory path to the service executable file cannot be found. |
| 21 | Invalid parameter | Invalid parameters have been passed to the service. |
| 22 | Service not started | The account that this service runs under is invalid or lacks the permissions to run the service. |
| 23..4294967295 | Other |
Call the Create method
review before running# Warning: this method can change the system. -WhatIf shows the operation without running it.
$methodParameters = @{
ClassName = 'Win32_ScheduledJob'
MethodName = 'Create'
Arguments = @{
Command = 'Example'
StartTime = (Get-Date)
RunRepeatedly = $false
DaysOfWeek = 1
DaysOfMonth = 1
InteractWithDesktop = $false
}
WhatIf = $true # Remove this line to actually run the method
}
$result = Invoke-CimMethod @methodParameters
# With -WhatIf nothing runs and no result is returned
if ($result) {
$returnValueName = @{
0 = 'Successful completion'
1 = 'Not supported'
2 = 'Access denied'
8 = 'Unknown failure'
9 = 'Path not found'
21 = 'Invalid parameter'
22 = 'Service not started'
}
$returnValueName[[int]$result.ReturnValue]
$result.JobId
}This method can change the system. The -WhatIf parameter shows the operation without running it: remove it only after checking.
Delete #
Class method that deletes a scheduled job.
uint32 Delete();Return values
| Value | Label | Description |
|---|---|---|
| 0 | Successful completion | The request was accepted. |
| 1 | Not supported | The request is not supported. |
| 2 | Access denied | The user did not have the necessary access. |
| 8 | Unknown failure | Interactive process. |
| 9 | Path not found | The directory path to the service executable file was not found. |
| 21 | Invalid parameter | Invalid parameters have been passed to the service. |
| 22 | Service not started | The account which this service is to run under is either invalid or lacks the permissions to run the service. |
| 23..4294967295 | Other |
Call the Delete method
review before running# Warning: this method can change the system. -WhatIf shows the operation without running it.
# Target instance
$instance = Get-CimInstance -ClassName Win32_ScheduledJob -Filter 'JobId = 0'
$methodParameters = @{
InputObject = $instance
MethodName = 'Delete'
WhatIf = $true # Remove this line to actually run the method
}
$result = Invoke-CimMethod @methodParameters
# With -WhatIf nothing runs and no result is returned
if ($result) {
$returnValueName = @{
0 = 'Successful completion'
1 = 'Not supported'
2 = 'Access denied'
8 = 'Unknown failure'
9 = 'Path not found'
21 = 'Invalid parameter'
22 = 'Service not started'
}
$returnValueName[[int]$result.ReturnValue]
}This method can change the system. The -WhatIf parameter shows the operation without running it: remove it only after checking.
Remarks
Each job scheduled against the schedule service is stored persistently (the scheduler can start a job after a reboot), and is executed at the specified time and day of the week or month. If the computer is not active, or if the scheduled service is not running at the specified job time, the schedule service runs the specified job on the next day at the specified time.
Jobs are scheduled according to Coordinated Universal Time (UTC) with bias offset from Greenwich Mean Time (GMT), which means that a job can be specified using any time zone. The Win32_ScheduledJob class returns the local time with UTC offset when enumerating an object, and converts to local time when creating new jobs. For example, a job specified to run on a computer in Boston at 10:30 P.M. Monday PST time will be scheduled to run locally at 1:30 A.M. Tuesday EST.
Note: A client must take into account whether or not daylight savings time is in operation on the local computer, and if it is, then subtract a bias of 60 minutes from the UTC offset.
The Win32_ScheduledJob class is derived from CIM_Job. You must be a member of the administrators group to create a scheduled job using this class.
The Win32_ScheduledJob class is internally using the AT protocol, which is bound to deprecation starting with Windows 8 and Windows Server 2012. As a first step the AT protocol is disabled by default. If the protocol is disabled, for example calling the Create method on a Win32_ScheduledJob object will fail with error 0x8. You can turn the AT protocol back on by adding the following registry entry:
``` syntax
Key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\Configuration
Name: EnableAt
Type: REG_DWORD
Value: 1
```
You may need to restart the machine to make the setting effective.
Because Win32_ScheduledJob is based on the NetScheduleJobGetInfo Win32 API, you cannot use this class in conjunction with the Task Scheduler. If you wish to use Task Scheduler, use the Task Scheduler API. For more information, see the Task Scheduler Reference.
Inheritance
Superclass chain
- CIM_ManagedSystemElement
- CIM_LogicalElement
- CIM_Job
- Win32_ScheduledJob
Known subclasses
No known subclass.
Requirements
- Namespace
- root/cimv2
- Minimum client
- Windows Vista
- Minimum server
- Windows Server 2008
- Provider
- CIMWin32
- MOF file
- CIMWin32.mof
- DLL
- CIMWin32.dll
Class qualifiers
Dynamic Provider("CIMWin32") UUID("{8502C4E0-5FBB-11D2-AAC1-006008C78BC7}") SupportsCreate CreateBy("Create") SupportsDelete DeleteBy("Delete") AMENDMENT
Microsoft source
Descriptions, properties and methods come from the Microsoft documentation (MicrosoftDocs/win32 repository), published under the CC BY 4.0 licence. Formatting and translations are specific to this site; the PowerShell examples are written for it.