Public/Test-specScheduledTask.ps1

function Test-specScheduledTask {
    <#
.SYNOPSIS
Checks for the existence of one or more scheduled tasks on the local system.
 
.DESCRIPTION
The Test-specScheduledTask function verifies whether the specified scheduled task(s) exist on the local system.
It searches within the default scheduled tasks root directory and, if specified, within a subfolder.
 
.PARAMETER TaskName
The name(s) of the scheduled task(s) to check for. This parameter is mandatory and accepts pipeline input.
 
.PARAMETER Folder
Optional. The folder name under the scheduled tasks root path where the task is located.
If not specified, the root path 'C:\Windows\System32\Tasks' is used directly.
 
.INPUTS
[string[]] You can pipe one or more task names or objects with matching property names to this function.
 
.OUTPUTS
[bool] Returns `$true` if the task exists, otherwise `$false`.
 
.EXAMPLE
Test-specScheduledTask -TaskName 'MyTask'
 
Checks if a scheduled task named 'MyTask' exists in the root task folder.
 
.EXAMPLE
Test-specScheduledTask -TaskName 'MyTask' -Folder 'MyFolder'
 
Checks if 'MyTask' exists in the 'MyFolder' subfolder under the scheduled tasks directory.
 
.EXAMPLE
'MyTask1', 'MyTask2' | Test-specScheduledTask
 
Pipes multiple task names into the function to check if they exist in the root folder.
 
.EXAMPLE
$tasks = @(
    [pscustomobject]@{taskname = 'psengine.launcher.device'; folder = 'MyCustomDirectory' }
    [pscustomobject]@{taskname = 'AnotherTaskName' }
    [pscustomobject]@{taskname = 'SideCarBehaviour - Reload'; folder = 'Microsoft\Intune' }
    [pscustomobject]@{taskname = 'Adobe Acrobat Update Task' }
)
 
$tasks | Test-specScheduledTask
 
Demonstrates how to pipe an array of custom objects into the function. Each object includes a 'taskname' and optionally a 'folder' property.
These map to the function's `TaskName` and `Folder` parameters via `ValueFromPipelineByPropertyName`.
 
.NOTES
    Author: owen.heaume
    Version: 1.0 - Initial Release
#>


    [cmdletBinding()]

    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [string[]]$TaskName,

        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [string]$Folder
    )

    begin {
        Write-Verbose "In function: $($MyInvocation.MyCommand.Name)"
        $taskRootPath = 'c:\windows\system32\Tasks'
    }

    process {
        foreach ($task in $TaskName) {
            Write-Verbose "Processing taskname: '$task'"

            if ($folder) {
                Write-Verbose "'Folder' parameter: '$folder' has been supplied in the function call"
                $fullPath = Join-Path $taskRootPath "$folder\$task"
                Write-Verbose "Full path to query task existence is: '$fullPath'"
                Test-Path $fullPath
            } else {
                Write-Verbose "'Folder' parameter has not been supplied in the function call"
                Write-Verbose "Full path to query task existence is: '$taskRootPath\$task'"
                Test-Path "$taskRootPath\$task"
            }
        }
    }

    end {
        Write-Verbose "Out of function: $($MyInvocation.MyCommand.Name)"
    }
}