Get scheduled task details and remove from remote machine

Get scheduled task details and remove from remote machine

Example

Function Get-ScheduledTask
{
param([string]$ComputerName = "localhost")
Write-Host "Computer: $ComputerName"
$Command = "schtasks.exe /query /s $ComputerName"
Invoke-Expression $Command
Clear-Variable Command -ErrorAction SilentlyContinue
Write-Host "`n"
}
# EXAMPLE: Get-ScheduledTask -ComputerName 127.0.0.1

Function Remove-ScheduledTask
{
param(
[string]$ComputerName = "localhost",
[string]$TaskName = "blank"
)
If ((Get-ScheduledTask -ComputerName $ComputerName) -match $TaskName)
{
If ((Read-Host "Are you sure you want to remove task $TaskName from $ComputerName(y/n)") -eq "y")
{
$Command = "schtasks.exe /delete /s $ComputerName /tn $TaskName /F"
Invoke-Expression $Command
Clear-Variable Command -ErrorAction SilentlyContinue
}
}
Else
{
Write-Warning "Task $TaskName not found on $ComputerName"
}
}


Remove-ScheduledTask -ComputerName 127.0.0.1 -TaskName notepad

Copy and Try it