@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem ============================================================
rem Connect to Wi-Fi network (Windows 10)
rem 1) Put the real password into WIFI_PASSWORD below.
rem 2) Run as administrator (system-wide profile, user=all).
rem The temporary profile file (contains the password) is
rem deleted right after the profile is added.
rem Security preset: WPA2-Personal (AES). If your network uses
rem plain WPA or WPA3, edit auth/encryption in the PowerShell
rem block near the bottom.
rem Do NOT put a double-quote character inside the password.
rem ============================================================
set "SSID=MSHR-MYT"
set "WIFI_PASSWORD=PUT_YOUR_PASSWORD_HERE"
rem --- validate placeholder / empty password ---
if not defined WIFI_PASSWORD goto :no_password
if "%WIFI_PASSWORD%"=="PUT_YOUR_PASSWORD_HERE" goto :no_password
rem --- require administrator rights ---
net session >nul 2>&1
if %errorlevel% neq 0 (
echo Requesting administrator privileges...
powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
exit /b
)
echo Connecting to Wi-Fi: %SSID%
echo.
powershell -NoProfile -ExecutionPolicy Bypass -Command "$ErrorActionPreference='Stop'; $ssid=$env:SSID; $pw=$env:WIFI_PASSWORD; if([string]::IsNullOrWhiteSpace($pw)){Write-Output 'Password is empty.'; exit 1}; $esc=[System.Security.SecurityElement]::Escape($pw); $nl=[Environment]::NewLine; $t=@('<MSProfile>',' <isGuestOrShared>false</isGuestOrShared>',' <name>'+$ssid+'</name>',' <SSIDConfig>',' <SSID>',' <name>'+$ssid+'</name>',' <type>2</type>',' </SSID>',' <autoConnectionMode>auto</autoConnectionMode>',' </SSIDConfig>',' <connectionMode>auto</connectionMode>',' <autoConnect>true</autoConnect>',' <security>',' <auth>WPA2PSK</auth>',' <encryption>AES</encryption>',' <keyMaterial>'+$esc+'</keyMaterial>',' </security>','</MSProfile>'); $xml=$t -join $nl; $tmp=Join-Path $env:TEMP ('wifi_'+[guid]::NewGuid().ToString('N')+'.xml'); try{ [IO.File]::WriteAllText($tmp,$xml,[Text.Encoding]::Unicode); & netsh wlan delete profile name=$ssid 2>$null | Out-Null; $add=& netsh wlan add profile filename=$tmp user=all; $addCode=$LASTEXITCODE; Write-Output ('add profile exit code: '+$addCode); if($add){Write-Output ($add -join ' ')}; if($addCode -ne 0){throw ('Failed to add Wi-Fi profile, exit code '+$addCode)}; $con=& netsh wlan connect name=$ssid; $conCode=$LASTEXITCODE; Write-Output ('connect exit code: '+$conCode); if($con){Write-Output ($con -join ' ')}; if($conCode -eq 0){Write-Output ('Connected to '+$ssid)}else{Write-Output ('Connect command returned '+$conCode)} } finally { if(Test-Path $tmp){Remove-Item $tmp -Force -ErrorAction SilentlyContinue; Write-Output 'Temporary profile file removed.'} }"
echo.
echo Current wireless interface status:
netsh wlan show interfaces
echo.
pause
exit /b 0
:no_password
echo [ERROR] Password is not set. Open this .bat file and replace
echo PUT_YOUR_PASSWORD_HERE with the real Wi-Fi password.
echo Do not use double-quote characters inside the password.
pause
exit /b 1