Remove-ScheduledScript.ps1


Description

Purpose

Removes a scheduled task from specified computers.

Detailed Description

The Remove-ScheduledScript function removes a scheduled task from specified computers. It checks if the task exists and unregisters it if found. If the task is not found, it displays an error message.

Back to Top

Usage

Example 1

Remove-ScheduledScript -TaskName "MyTask" -ComputerName "RemoteComputer1", "RemoteComputer2"

This example removes the scheduled task named “MyTask” from the remote computers named “RemoteComputer1” and “RemoteComputer2”.

Example 2

"RemoteComputer1", "RemoteComputer2" | Remove-ScheduledScript -TaskName "MyTask"

This example removes the scheduled task named “MyTask” from the remote computers named “RemoteComputer1” and “RemoteComputer2”.

Back to Top

Notes

Author: Your Name Date: Today’s Date

Back to Top


Script

<#
.SYNOPSIS
Removes a scheduled task from specified computers.

.DESCRIPTION
The Remove-ScheduledScript function removes a scheduled task from specified computers. It checks if the task exists and unregisters it if found. If the task is not found, it displays an error message.

.PARAMETER TaskName
The name of the scheduled task to be removed.

.PARAMETER ComputerName
The names of the computers from which the scheduled task should be removed. The default value is the local computer.

.PARAMETER Credential
Specifies a user account that has permission to perform the operation on the remote computers. If not specified, the current user's credentials are used.

.EXAMPLE
Remove-ScheduledScript -TaskName "MyTask" -ComputerName "RemoteComputer1", "RemoteComputer2"

This example removes the scheduled task named "MyTask" from the remote computers named "RemoteComputer1" and "RemoteComputer2".

.EXAMPLE
"RemoteComputer1", "RemoteComputer2" | Remove-ScheduledScript -TaskName "MyTask"

This example removes the scheduled task named "MyTask" from the remote computers named "RemoteComputer1" and "RemoteComputer2".

.INPUTS
System.String.

.OUTPUTS
None.

.NOTES
Author: Your Name
Date: Today's Date

.LINK
https://docs.microsoft.com/en-us/powershell/module/scheduledtasks/unregister-scheduledtask?view=windowsserver2019-ps
#>
function Remove-ScheduledScript {
    [CmdletBinding(SupportsShouldProcess = $true)]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [string]
        $TaskName,
        
        [Parameter(Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [string[]]
        $ComputerName = $env:COMPUTERNAME,

        [Parameter(Mandatory = $false)]
        [System.Management.Automation.PSCredential]
        $Credential
    )

    process {
        foreach ($computer in $ComputerName) {
            if ($PSCmdlet.ShouldProcess("$TaskName on $computer", "Remove scheduled task")) {
                $scriptBlock = {
                    param ($TaskName)
                    try {
                        if (Get-ScheduledTask -TaskName $TaskName -TaskPath \ -ErrorAction SilentlyContinue) {
                            Unregister-ScheduledTask -TaskName "$TaskName" -Confirm:$false
                        }
                        else {
                            Write-Error "No scheduled task found with the name $TaskName"
                        }
                    }
                    catch {
                        Write-Error "Failed to remove scheduled task: $_"
                    }
                }

                if ($computer -eq $env:COMPUTERNAME) {
                    & $scriptBlock -TaskName $TaskName
                }
                else {
                    $sessionParams = @{
                        ComputerName = $computer
                    }
                    if ($Credential) {
                        $sessionParams.Credential = $Credential
                    }
                    $session = New-PSSession @sessionParams
                    Invoke-Command -Session $session -ScriptBlock $scriptBlock -ArgumentList $TaskName
                    Remove-PSSession -Session $session
                }
            }
        }
    }
}

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top