Public/Wifi/Remove-WiFiConnectionProfile.ps1

Function Remove-WifiConnectionProfile {
    <#
.SYNOPSIS
    Removes specified Wi-Fi connection profiles from the system.
 
.DESCRIPTION
    This function deletes one or more Wi-Fi connection profiles. If no profiles are
    specified, it tries to find the profile(s) by the provided name (`ProfileName`).
    The function performs a check to ensure the profile can be deleted before attempting
    to remove it. It also handles timeout scenarios for the deletion process.
 
.PARAMETER Profiles
    Optional. An array of Wi-Fi connection profiles to delete. If not provided,
    the function will attempt to find the profile(s) using the `ProfileName` parameter.
 
.PARAMETER ProfileName
    Optional. The name of the Wi-Fi connection profile(s) to delete. If not provided,
    the function will use the `Profiles` parameter to identify profiles for removal.
 
.PARAMETER TimeoutMS
    Optional. The timeout duration (in milliseconds) for deleting the connection profiles.
    Defaults to 10,000 milliseconds (10 seconds).
 
.EXAMPLE
    Remove-WifiConnectionProfile -ProfileName "HomeWiFi"
    Removes the Wi-Fi profile named "HomeWiFi".
 
.EXAMPLE
    Get-WifiConnectionProfile | Remove-WifiConnectionProfile
    Removes all Wi-Fi connection profiles retrieved by the `Get-WifiConnectionProfile` function.
 
.NOTES
    - The function checks if the profile is deletable (`CanDelete`) before attempting removal.
    - If no matching profiles are found, a warning is issued, and the function exits gracefully.
    - The function supports timeouts and will warn if the deletion exceeds the specified timeout duration.
#>

    param(
        [parameter(Mandatory = $False, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias("Profile")]
        [Windows.Networking.Connectivity.ConnectionProfile[]]$Profiles,
        [String]$ProfileName,
        [int]$TimeoutMS = 10000 # Nu netjes als laatste parameter
    )
    begin {
        if ((-not $Profiles) -and $PSBoundParameters.ContainsKey("ProfileName")) {
            $Profiles = Get-WifiConnectionProfile | Where-Object { $_.ProfileName -eq $ProfileName }
        }
        # Check if Profiles is still empty after the fetch
        if (-not $Profiles) {
            Write-Warning "No Wi-Fi profile found with the name: '$($ProfileName)'. Exiting without any action."
            return # Graceful exit if no profiles are found
        }
    }
    process {
        foreach ($P in $Profiles) {
            if ($P.CanDelete) {
                Write-Host ("Removing Profile {0}" -f $P.ProfileName) -ForegroundColor Cyan
                # De WinRT call klaarzetten
                $task = $P.TryDeleteAsync()
                $resultType = [Windows.Networking.Connectivity.ConnectionProfileDeleteStatus]
                # Await aanroepen met de gekozen timeout
                $status = Await -WinRtTask $task -ResultType $resultType -TimeoutMs $TimeoutMS
                if ($status -eq "Success") {
                    Write-Host "Profile Succesfull removed" -ForegroundColor Green
                } elseif ($null -eq $status) {
                    Write-Warning "Timeout reached for removing profile: $($P.ProfileName)."
                } else {
                    Write-Error "Error during remove: $($status)"
                }
            } else {
                Write-Warning ("Cant Remove Profile {0}" -f $P.ProfileName)
            }
        }
    }
}
# Register the argument completer for the 'ProfileName' parameter of 'Remove-WifiConnectionProfile'
Register-ArgumentCompleter -CommandName 'Remove-WifiConnectionProfile' -ParameterName 'ProfileName' -ScriptBlock $ScriptBlockArgumentCompleterProfileName