public/Remove-LimitFileSize.ps1

function Remove-LimitFileSize {
    <#
    .SYNOPSIS
        Removes the scheduled task named "WatchLimitFileSize" if it exists.

    .DESCRIPTION
        This function checks for the existence of a scheduled task named "WatchLimitFileSize"
        and removes it if present.

    .PARAMETER VerboseLevel
        Controls the script's verbosity level :
            - "Disabled": no console output.
            - "Debug": detailed output.

    .OUTPUTS
        System.Boolean
        Returns $true if the task was found and removed, $false if it didn't exist.

    .EXAMPLE
        Remove-LimitFileSize -VerboseLevel Debug

        Removes the task with debug output enabled.

    .NOTES
        Version : 1.0.2
        Author : Frederic PETIT
        Created : 2025-06-05
        Revised : 2025-06-05

        Compatibility: PowerShell 5+
    #>


    [CmdletBinding(SupportsShouldProcess = $true)]
    Param (
        [Parameter(Mandatory=$false)][ValidateSet("Disabled", "Debug")][string]$VerboseLevel = "Disabled"
    )

    # Nom de la tâche planifiée à supprimer.
    $TaskName = "WatchLimitFileSize";
    # Tente de récupérer la tâche planifiée ; retourne $null si elle n'existe pas (aucune erreur affichée).
    $task = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue;

    # Test : tâche existe.
    if ($null -ne $task) {
        try {
            Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false;
            if ($VerboseLevel -eq "Debug") {
                Write-Host "Task '$TaskName' removed." -ForegroundColor Green;
            }
            return $true;
        } catch {
            if ($VerboseLevel -eq "Debug") {
                Write-Warning "Error while removing task '$TaskName': $_";
            }
            return $false;
        }
    } else {
        if ($VerboseLevel -eq "Debug") {
            Write-Host "Task '$TaskName' does not exist. Nothing to remove." -ForegroundColor Green;
        }
        return $false;
    }
}