Remove-DbaAgentJobSchedule
View SourceSynopsis
Detaches a schedule from a SQL Server Agent job without removing the schedule.
Description
Detaches one or more schedules from a SQL Server Agent job without deleting the schedule itself. This is equivalent to executing sp_detach_schedule in T-SQL.
This is particularly useful when a schedule is shared between multiple jobs and you need to stop a specific job from running on that schedule without affecting other jobs that use the same schedule. The schedule remains in SQL Server Agent and can be reattached to the job or attached to other jobs at any time.
Use Set-DbaAgentJob with the -Schedule parameter to reattach a schedule to a job after detaching it.
Syntax
Remove-DbaAgentJobSchedule
[[-SqlInstance] <DbaInstanceParameter[]>]
[[-SqlCredential] <PSCredential>]
[[-Job] <String[]>]
[-Schedule] <String[]>
[[-InputObject] <Job[]>]
[-EnableException]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Examples
Example: 1
PS C:\> Remove-DbaAgentJobSchedule -SqlInstance sql1 -Job Job1 -Schedule SharedSchedule
Detaches the schedule named ‘SharedSchedule’ from job ‘Job1’ on sql1. The schedule itself is not deleted and remains available for other jobs.
Example: 2
PS C:\> Remove-DbaAgentJobSchedule -SqlInstance sql1 -Job Job1 -Schedule Schedule1, Schedule2
Detaches multiple schedules from a single job on sql1.
Example: 3
PS C:\> Remove-DbaAgentJobSchedule -SqlInstance sql1, sql2 -Job Job1 -Schedule SharedSchedule
Detaches the schedule from job ‘Job1’ on multiple SQL Server instances.
Example: 4
PS C:\> Get-DbaAgentJob -SqlInstance sql1 -Job Job1 | Remove-DbaAgentJobSchedule -Schedule SharedSchedule
Detaches the schedule ‘SharedSchedule’ from job ‘Job1’ using pipeline input.
Example: 5
PS C:\> Get-DbaAgentJob -SqlInstance sql1 | Where-Object Name -like 'Maintenance*' | Remove-DbaAgentJobSchedule -Schedule SharedSchedule
Detaches ‘SharedSchedule’ from all jobs whose names start with ‘Maintenance’ on sql1.
Required Parameters
-Schedule
The name of the schedule(s) to detach from the job. The schedule itself is not deleted; only the association between the job and schedule is removed.
| Property | Value |
|---|---|
| Alias | |
| Required | True |
| Pipeline | false |
| Default Value |
Optional Parameters
-SqlInstance
The target SQL Server instance or instances. This can be a collection and receive pipeline input to allow the function to be executed against multiple SQL Server instances.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value |
-SqlCredential
Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value |
-Job
The name of the SQL Agent job(s) from which to detach the schedule. Required when using -SqlInstance.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value |
-InputObject
Accepts job objects from the pipeline, typically from Get-DbaAgentJob output. Use this when you want to filter or retrieve jobs first, then pipe the results for schedule detachment.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | true (ByValue) |
| Default Value |
-EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with “sea of red” exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this “nice by default” feature off and enables you to catch exceptions with your own try/catch.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value | False |
-WhatIf
If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.
| Property | Value |
|---|---|
| Alias | wi |
| Required | False |
| Pipeline | false |
| Default Value |
-Confirm
If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.
| Property | Value |
|---|---|
| Alias | cf |
| Required | False |
| Pipeline | false |
| Default Value |
Outputs
PSCustomObject
Returns one object per detach operation, containing the result and details.
Properties:
- ComputerName: The computer name of the SQL Server instance
- InstanceName: The SQL Server instance name
- SqlInstance: The full SQL Server instance name (computer\instance)
- Job: The name of the job from which the schedule was detached
- Schedule: The name of the schedule that was detached
- ScheduleId: The numeric ID of the schedule
- ScheduleUid: The unique GUID identifier of the schedule
- Status: Result of the operation (“Detached” for success, or error message for failures)
- IsDetached: Boolean indicating if the schedule was successfully detached
dbatools