Public/Wifi/Disconnect-WifiNetwork.ps1

Function Disconnect-WifiNetwork {
  <#
.SYNOPSIS
    Disconnects Wi-Fi adapters from their currently connected networks.
 
.DESCRIPTION
    This function disconnects one or more Wi-Fi adapters from their active connections.
    If no specific adapter index is provided, all available adapters will be disconnected.
    If an invalid index is provided, a warning is issued.
 
.PARAMETER Index
    Optional. The index or indices of the Wi-Fi adapters to disconnect. If not provided,
    all available adapters will be disconnected.
 
.EXAMPLE
    Disconnect-WifiNetwork -Index 0
    Disconnects the Wi-Fi adapter at index 0 from its network.
 
.EXAMPLE
    Disconnect-WifiNetwork
    Disconnects all available Wi-Fi adapters from their networks.
#>

  param(
    [Parameter(Mandatory = $false, Position = 0)]
    [int[]]$Index
  )
  Process {
    $Adapters = Get-WiFiAvailableAdapter
    if ($null -eq $Adapters -or $Adapters.Count -eq 0) {
      Write-Warning "No WiFi adapters found to disconnect."
      return
    }
    $Targets = if ($PSBoundParameters.ContainsKey('Index')) {
      foreach ($i in $Index) {
        if ($i -ge 0 -and $i -lt $Adapters.Count) { $Adapters[$i] }
        else { Write-Warning "Index $i skipped: does not exist (range: 0 to $($Adapters.Count - 1))." }
      }
    } else {
      $Adapters
    }
    foreach ($A in $Targets) {
      try {
        $A.Disconnect()
        Write-Host "Disconnected adapter: $($A.NetworkAdapter.NetworkAdapterId)" -ForegroundColor Green
      } catch {
        Write-Error "Failed to disconnect: $($_.Exception.Message)"
      }
    }
  }
}
Register-ArgumentCompleter -CommandName 'Disconnect-WifiNetwork' -ParameterName 'Index' -ScriptBlock $ScriptBlockArgumentCompleterIndex