Functions/Remove-FileFromSharePointOnline.ps1

<#
.SYNOPSIS
    This function removes a file from SharePoint Online.
.DESCRIPTION
    This function removes a file from SharePoint Online.
#>

function Remove-FileFromSharePointOnline {
    [CmdletBinding(PositionalBinding=$false)]
    [OutputType([Bool])]
    param (
        # The path to the file on SharePoint Online.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$filePath,

        # The SharePoint Online site url where the file will be removed from.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$siteUrl,

        # The MSPComplete Endpoint containing the Microsoft Graph credentials.
        [Parameter(Mandatory=$true, ParameterSetName="endpoint", ValueFromPipeline=$true)]
        [ValidateNotNull()]
        $endpoint,

        # The Microsoft Graph authentication token.
        [Parameter(Mandatory=$true, ParameterSetName="token")]
        [ValidateNotNullOrEmpty()]
        [String]$token,

        # Select the stream where the messages will be directed.
        [Parameter(Mandatory=$false)]
        [ValidateSet("Information", "Warning", "Error")]
        [String]$outputStream = "Error"
    )

    # Retrieve the Microsoft Graph authentication token
    if ($PSCmdlet.ParameterSetName -eq "endpoint") {
        Write-Information "Retrieving the Microsoft Graph authentication token using the provided endpoint."
        $token = Get-MicrosoftGraphAuthenticationToken -Endpoint $endpoint
        if ([String]::IsNullOrWhiteSpace($token)) {
            Write-OutputMessage "Failed to retrieve the Microsoft Graph authentication token using the provided endpoint." -OutputStream $outputStream -ReturnMessage:$false
            return $false
        }
    }

    # Set the protocol to TLS 1.2
    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

    # Remove any "\" or "/" at the start of the SharePoint Online file path
    if ($filePath.StartsWith("\")) {
        $filePath = $filePath.TrimStart("\")
    }
    if ($filePath.StartsWith("/")) {
        $filePath = $filePath.TrimStart("/")
    }

    # Verify that the file path is valid
    if ([String]::IsNullOrWhiteSpace($filePath)) {
        Write-OutputMessage "Failed to retrieve a valid file path from '$($filePath)'." -OutputStream $outputStream -ReturnMessage:$false
        return $false
    }

    # Verify that the siteUrl is valid
    if (!(Test-DomainValidity -Domain $siteUrl)) {
        Write-OutputMessage "The siteUrl '$($siteUrl)' is not valid." -OutputStream $outputStream -ReturnMessage:$false
        return $false
    }

    # Prepare the DELETE Request
    $invokeRestMethodParams = @{
        Uri     = "https://graph.microsoft.com/v1.0/sites/$($siteUrl)/drive/root:/$($filePath)"
        Method  = "DELETE"
        Headers = @{
            Accept        = "application/json"
            Authorization = "bearer $($token)"
        }
    }

    # Try to remove the file from SharePoint Online
    Write-Information "Removing the file '$($filePath)' from SharePoint Online."
    try {
        $response = Invoke-RestMethod @invokeRestMethodParams
    }
    catch {
        Write-OutputMessage "Exception occurred while removing the file '$($filePath)' from SharePoint Online. `r`n$($_.Exception.Message)" -OutputStream $outputStream -ReturnMessage:$false
        return $false
    }

    # If the deletion is successful, $response will be an empty string, if not, it will be null
    if ("" -eq $response) {
        return $true
    }

    # Failed to remove the file
    else {
        Write-OutputMessage "Failed to remove the file '$($filePath)'." -OutputStream $outputStream -ReturnMessage:$false
        return $false
    }
}