Create

Class method that submits a job to the operating system for execution at a specified future time and date.

Class method that submits a job to the operating system for execution at a specified future time and date.

Example

Do not run below example code just to see what happens next. Many methods seriously affect your system. Always make sure you actually understand what the method and the code do.

# define the arguments you want to submit to the method
# remove values that you do not want to submit
# make sure you replace values with meaningful content before running the code
# see section "Parameters" below for a description of each argument.
$arguments = @{
    Command         = 'someText'  # replace 'someText' with meaningful text
    DaysOfMonth     = [UInt32](12345)  # replace 12345 with a meaningful value
    DaysOfWeek      = [UInt32](12345)  # replace 12345 with a meaningful value
    InteractWithDesktop = [Boolean](12345)  # replace 12345 with a meaningful value
    RunRepeatedly   = [Boolean](12345)  # replace 12345 with a meaningful value
    StartTime       = [DateTime](12345)  # replace 12345 with a meaningful value
}


Invoke-CimMethod -ClassName Win32_ScheduledJob -Namespace Root/CIMV2 -MethodName Create -Arguments $arguments |
Add-Member -MemberType ScriptProperty -Name ReturnValueFriendly -Passthru -Value {
  switch ([int]$this.ReturnValue)
  {
        0        {'Successful completion'}
        1        {'Not supported'}
        2        {'Access denied'}
        8        {'Unknown failure'}
        9        {'Path not found'}
        21       {'Invalid parameter'}
        22       {'Service not started'}
        default  {'Unknown Error '}
    }
}

To run this method on one or more remote systems, use New-CimSession:

$ComputerName = 'server12','server14'  # adjust to your server names
$Credential   = Get-Credential         # submit a user account with proper permissions

# define the arguments you want to submit to the method
# remove values that you do not want to submit
# make sure you replace values with meaningful content before running the code
# see section "Parameters" below for a description of each argument.
$arguments = @{
    Command         = 'someText'  # replace 'someText' with meaningful text
    DaysOfMonth     = [UInt32](12345)  # replace 12345 with a meaningful value
    DaysOfWeek      = [UInt32](12345)  # replace 12345 with a meaningful value
    InteractWithDesktop = [Boolean](12345)  # replace 12345 with a meaningful value
    RunRepeatedly   = [Boolean](12345)  # replace 12345 with a meaningful value
    StartTime       = [DateTime](12345)  # replace 12345 with a meaningful value
}


$session = New-CimSession -ComputerName $ComputerName -Credential $Credential

Invoke-CimMethod -ClassName Win32_ScheduledJob -Namespace Root/CIMV2 -MethodName Create -Arguments $arguments -CimSession $session |
Add-Member -MemberType ScriptProperty -Name ReturnValueFriendly -Passthru -Value {
  switch ([int]$this.ReturnValue)
  {
        0        {'Successful completion'}
        1        {'Not supported'}
        2        {'Access denied'}
        8        {'Unknown failure'}
        9        {'Path not found'}
        21       {'Invalid parameter'}
        22       {'Service not started'}
        default  {'Unknown Error '}
    }
}


Remove-CimSession -CimSession $session

Learn more about Invoke-CimMethod and invoking WMI methods.

Syntax

uint32 Create(
  [in]           string   Command,
  [in]           datetime StartTime,
  [in, optional] boolean  RunRepeatedly,
  [in, optional] uint32   DaysOfWeek,
  [in, optional] uint32   DaysOfMonth,
  [in, optional] boolean  InteractWithDesktop,
  [out]          uint32   JobId
);

Parameters

Name Type Description
Command String The Command parameter contains the name of the command, batch program, or binary file (along with command line parameters) that the schedule service will use to invoke the job.
Example: defrag /q /f
DaysOfMonth UInt32 The DaysOfMonth parameter indicates the days of the month when the job is scheduled to run, and is used only when the RunRepeatedly parameter is TRUE.
DaysOfWeek UInt32 The DaysOfWeek parameter indicates the days of the week when the job is scheduled to run, and is used only when the RunRepeatedly parameter is TRUE.
InteractWithDesktop Boolean The InteractWithDesktop parameter indicates whether the specified job should be interactive (meaning a user can give input to the scheduled job while it is executing).
Values TRUE or FALSE. If TRUE, then the job will be interactive. The default value of this parameter is FALSE.
RunRepeatedly Boolean The RunRepeatedly parameter indicates whether the scheduled job should run repeatedly on the days that the job is scheduled.
Values: TRUE or FALSE. If TRUE, then the job is run repeatedly according to the DaysOfWeek and DaysOfMonth parameter. If no value is supplied the parameter defaults to FALSE.
StartTime DateTime The StartTime parameter represents the UTC time to run the job. This is of the form YYYYMMDDHHMMSS.MMMMMM(+-)OOO, where YYYYMMDD must be replaced by **.
Example: **
**123000.000000-420 which implies 12:30 pm PST with daylight savings time in effect.

Return Value

Returns a value of type UInt32. Return values:

$returnValues = @{
    0    = 'Successful completion'
    1    = 'Not supported'
    2    = 'Access denied'
    8    = 'Unknown failure'
    9    = 'Path not found'
    21   = 'Invalid parameter'
    22   = 'Service not started'
}

Requirements

To use Win32_ScheduledJob, the following requirements apply:

PowerShell

Get-CimInstance was introduced with PowerShell Version 3.0, which in turn was introduced on clients with Windows 8 and on servers with Windows Server 2012.

If necessary, update Windows PowerShell to Windows PowerShell 5.1, or install PowerShell 7 side-by-side.

Operating System

Win32_ScheduledJob was introduced on clients with Windows Vista and on servers with Windows Server 2008.

Namespace

Win32_ScheduledJob lives in the Namespace Root/CIMV2. This is the default namespace. There is no need to use the -Namespace parameter in Get-CimInstance.

Implementation

Win32_ScheduledJob is implemented in CIMWin32.dll and defined in CIMWin32.mof. Both files are located in the folder C:\Windows\system32\wbem:

explorer $env:windir\system32\wbem
notepad $env:windir\system32\wbem\CIMWin32.mof