Public/HotSpot/WifiHotSpot.ps1

$null = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager, Windows.Networking.NetworkOperators, ContentType = WindowsRuntime]
$null = [Windows.Networking.NetworkOperators.TetheringWiFiBand, Windows.Networking.NetworkOperators, ContentType = WindowsRuntime]
$null = [Windows.Networking.NetworkOperators.TetheringWiFiAuthenticationKind, Windows.Networking.NetworkOperators, ContentType = WindowsRuntime]
$null = [Windows.Networking.Connectivity.NetworkInformation, Windows.Networking.Connectivity, ContentType = WindowsRuntime]


function Set-WifiHotSpot {
    <#
.SYNOPSIS
    Manages the Wi-Fi hotspot configuration on a Windows device, including setting SSID, password, authentication, and band, as well as starting, stopping, and resetting the hotspot.
 
.DESCRIPTION
    This function allows you to configure and manage your device's Wi-Fi hotspot. You can customize the SSID, password, Wi-Fi band, and authentication type. It also supports starting and stopping the hotspot, as well as resetting it to default settings.
 
.PARAMETER SSID
    The SSID (network name) to assign to the Wi-Fi hotspot.
 
.PARAMETER Password
    The password to use for the Wi-Fi hotspot.
 
.PARAMETER Band
    The Wi-Fi band to use. Valid values are:
    - `Auto` (Automatically selects the best band)
    - `TwoPointFourGigahertz` (Uses 2.4 GHz band)
    - `FiveGigahertz` (Uses 5 GHz band)
    - `SixGigahertz` (Uses 6 GHz band)
 
.PARAMETER Start
    A switch to start the Wi-Fi hotspot.
 
.PARAMETER Stop
    A switch to stop the Wi-Fi hotspot.
 
.PARAMETER AuthenticationKind
    The authentication type to use. Valid values are:
    - `Wpa2` (Wi-Fi Protected Access 2)
    - `Wpa3TransitionMode` (Supports both WPA2 and WPA3 clients)
    - `Wpa3` (Wi-Fi Protected Access 3, the latest and most secure standard)
 
.PARAMETER Reset
    A switch to reset the hotspot configuration to its default settings.
 
.EXAMPLE
    Set-WifiHotSpot -SSID "MyHotspot" -Password "SecurePassword123" -Band "FiveGigahertz" -AuthenticationKind "Wpa2" -Start
    Starts the Wi-Fi hotspot with the specified SSID, password, and authentication type on the 5 GHz band.
 
.EXAMPLE
    Set-WifiHotSpot -Reset
    Resets the Wi-Fi hotspot configuration to its default settings.
 
.NOTES
#>

    param(
        [string]$SSID,
        [string]$Password,
        [Windows.Networking.NetworkOperators.TetheringWiFiBand]$Band,
        [switch]$Start,
        [switch]$Stop,
        [Windows.Networking.NetworkOperators.TetheringWiFiAuthenticationKind]$AuthenticationKind,
        [switch]$Reset
    )
    # 1. Manager Setup
    $netProfile = [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile()
    if (-not $netProfile) { Write-Error "Geen actieve internetverbinding gevonden."; return }
    
    $tetheringManager = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager]::CreateFromConnectionProfile($netProfile)
    $current = $tetheringManager.GetCurrentAccessPointConfiguration()

    # 2. Configuratie Logica
    $isUpdating = ( $PSBoundParameters.ContainsKey('SSID') -or 
        $PSBoundParameters.ContainsKey('Password') -or 
        $PSBoundParameters.ContainsKey('Band') -or 
        $PSBoundParameters.ContainsKey('AuthenticationKind'))

    if ($isUpdating) {
        $newConfig = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringAccessPointConfiguration]::new()
        $newConfig.Ssid = if ($PSBoundParameters.ContainsKey('SSID')) { $SSID } else { $current.Ssid }
        $newConfig.Passphrase = if ( $PSBoundParameters.ContainsKey('Password')) { $Password } else { $current.Passphrase }
        $newConfig.AuthenticationKind = if ( $PSBoundParameters.ContainsKey('AuthenticationKind')) { $AuthenticationKind } else { $current.AuthenticationKind }
        $newConfig.Band = if ( $PSBoundParameters.ContainsKey('Band')) { $Band } Else { $current.Band }
        $tetheringManager.ConfigureAccessPointAsync($newConfig).GetResults() | Out-Null
    }
    if ($PSBoundParameters.ContainsKey('Reset')) {
        # Setting is stored in
        # HKLM:\SYSTEM\CurrentControlSet\Services\icssvc\Settings\PrivateConnectionSettings
        # Specificly this key can be removed without problems ( check for your self but dont trust what i say !)
        # Removing this key will trully reset the hotspot config
        $tetheringManager.StopTetheringAsync().GetResults() | Out-Null
        $resetConfig = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringAccessPointConfiguration]::new()
        $resetConfig.Ssid = $env:COMPUTERNAME
        $resetConfig.Passphrase = ( -join ((48..57) + (65..90) + (97..122) | Get-Random -Count 12 | ForEach-Object { [char]$_ }))
        $resetConfig.Band = [Windows.Networking.NetworkOperators.TetheringWiFiBand]::Auto
        $tetheringManager.ConfigureAccessPointAsync($resetConfig).GetResults() | Out-Null
    }
    # 4. Acties
    if ($Start) { $tetheringManager.StartTetheringAsync().GetResults() | Out-Null }
    if ($Stop) { $tetheringManager.StopTetheringAsync().GetResults() | Out-Null }

    # 5. Status Output
    $final = $tetheringManager.GetCurrentAccessPointConfiguration()
    return [PSCustomObject]@{
        Status             = $tetheringManager.TetheringOperationalState
        SSID               = $final.Ssid
        Password           = $final.Passphrase
        Band               = $final.Band
        AuthenticationKind = $final.AuthenticationKind
        Limit              = $tetheringManager.MaxClientCount
        Connected          = $tetheringManager.ClientCount
    }
}
$ScriptBlockArgumentWifiHotspotBand = {
    param ($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
    $enumType = [Windows.Networking.NetworkOperators.TetheringWiFiBand]
    [Enum]::GetNames($enumType) | Where-Object { $_ -like "$wordToComplete*" }
}
$ScriptBlockArgumentWifiHotSpotAuthenticationKind = {
    param ($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
    $enumType = [Windows.Networking.NetworkOperators.TetheringWiFiAuthenticationKind]
    [Enum]::GetNames($enumType) | Where-Object { $_ -like "$wordToComplete*" }
}
Register-ArgumentCompleter -CommandName 'Set-WifiHotspot' -ParameterName 'Band' -ScriptBlock $ScriptBlockArgumentWifiHotspotBand
Register-ArgumentCompleter -CommandName 'Set-WifiHotspot' -ParameterName 'AuthenticationKind' -ScriptBlock $ScriptBlockArgumentWifiHotspotAuthenticationKind