Skip to main content

How to Create an Ad-Hoc Wi-Fi Hotspot in Batch Script

Sometimes you need to share your laptop's internet connection with other devices, or create a small local network for file transfers without a router. Windows includes a feature called the Hosted Network that allows your Wi-Fi card to act as a virtual access point. While modern Windows 10/11 has a "Mobile Hotspot" toggle in the settings GUI, a Batch script provides more direct control over the SSID and security key, making it ideal for kiosks or automated laboratory environments where a persistent, predictable hotspot is required.

warning

The legacy netsh wlan hostednetwork feature has been removed from many modern Wi-Fi drivers (those using the WDI driver model). If your hardware doesn't support it, see Method 4 for the modern PowerShell alternative using Start-NetEventSession or the Mobile Hotspot API.

This guide will explain how to configure and start a virtual Wi-Fi hotspot.

Method 1: Configuring and Starting the Hosted Network​

We use the netsh wlan command to set the identity and security of the network.

@echo off
setlocal enabledelayedexpansion

set "SSID=MyAutomatedHotspot"
set "Pass=Password123!"

:: Check for Administrator privileges
net session >nul 2>&1
if !errorlevel! neq 0 (
echo [ERROR] This script requires Administrator privileges.
echo Right-click and select "Run as administrator."
pause
endlocal
exit /b 1
)

:: Verify the Wi-Fi adapter supports Hosted Network
echo [CHECK] Verifying Hosted Network support...
echo.

set "Supported=0"
for /f "tokens=2 delims=:" %%a in ('netsh wlan show drivers 2^>nul ^| findstr /i "Hosted network supported"') do (
set "result=%%a"
:: Remove leading space
for /f "tokens=*" %%b in ("!result!") do set "result=%%b"
if /i "!result!"=="Yes" set "Supported=1"
)

if !Supported! equ 0 (
echo [ERROR] Your Wi-Fi adapter does NOT support Hosted Network.
echo This is common with modern WDI drivers.
echo.
echo Alternatives:
echo - Use Windows Settings ^> Mobile Hotspot
echo - See Method 4 for the PowerShell approach
pause
endlocal
exit /b 1
)

echo [OK] Hosted Network is supported.
echo.

:: Validate password length (WPA2 requires 8-63 characters)
set "PassLen=0"
set "tempPass=!Pass!"
:CountPass
if defined tempPass (
set "tempPass=!tempPass:~1!"
set /a PassLen+=1
goto :CountPass
)

if !PassLen! lss 8 (
echo [ERROR] Password must be at least 8 characters for WPA2 security.
pause
endlocal
exit /b 1
)

:: 1. Set the mode, SSID, and key
echo [ACTION] Configuring Wi-Fi Hotspot...
netsh wlan set hostednetwork mode=allow ssid="%SSID%" key="%Pass%" >nul 2>&1

if !errorlevel! neq 0 (
echo [ERROR] Failed to configure hosted network.
pause
endlocal
exit /b 1
)

:: 2. Start the network
echo [ACTION] Starting the broadcast...
netsh wlan start hostednetwork >nul 2>&1

if !errorlevel! equ 0 (
echo.
echo ==========================================
echo HOTSPOT IS NOW ACTIVE
echo ==========================================
echo SSID: %SSID%
echo Password: %Pass%
echo Security: WPA2-PSK
echo ==========================================
echo.
echo [WARN] Internet sharing is NOT automatic.
echo To share your internet connection:
echo 1. Open ncpa.cpl
echo 2. Right-click your internet adapter ^(Ethernet/Wi-Fi^)
echo 3. Properties ^> Sharing tab
echo 4. Check "Allow other network users to connect"
echo 5. Select the new virtual adapter
) else (
echo [ERROR] Failed to start hotspot.
echo Possible causes:
echo - Wi-Fi adapter is disabled
echo - Another hotspot is already running
echo - Driver doesn't support this feature
echo.
echo [TIP] Try: netsh wlan stop hostednetwork
echo Then run this script again.
)

:: Log the action
echo [%date% %time%] STARTED hotspot SSID=%SSID% by %USERNAME% >> "%USERPROFILE%\hotspot_log.txt"

pause
endlocal

Method 2: Stopping the Hotspot​

Shuts down the network and hides your SSID.

@echo off
setlocal

:: Check for Administrator privileges
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] This script requires Administrator privileges.
pause
endlocal
exit /b 1
)

echo [ACTION] Stopping the Wi-Fi Hotspot...

:: Check if a hosted network is running
netsh wlan show hostednetwork 2>nul | findstr /i "Started" >nul 2>&1
if %errorlevel% neq 0 (
echo [INFO] No active hotspot detected.
pause
endlocal
exit /b 0
)

:: Stop the network
netsh wlan stop hostednetwork >nul 2>&1

if %errorlevel% equ 0 (
echo [SUCCESS] Hotspot has been stopped.
) else (
echo [ERROR] Failed to stop hotspot.
)

:: Optionally disable the hosted network entirely
echo.
set /p "DisableChoice=Disable Hosted Network mode entirely? (Y/N): "
if /i "%DisableChoice%"=="Y" (
netsh wlan set hostednetwork mode=disallow >nul 2>&1
echo [OK] Hosted Network mode has been disabled.
)

:: Log
echo [%date% %time%] STOPPED hotspot by %USERNAME% >> "%USERPROFILE%\hotspot_log.txt"

pause
endlocal

Method 3: Auditing Connected Devices​

Once your hotspot is running, see who has connected and the network status.

@echo off
setlocal

echo [REPORT] Hotspot Status and Connected Clients
echo.

:: Check if hosted network is supported
netsh wlan show hostednetwork 2>nul | findstr /i "Not" >nul 2>&1
if %errorlevel% equ 0 (
echo [INFO] Hosted Network is not configured or not supported.
pause
endlocal
exit /b 0
)

:: Display full status
echo === HOSTED NETWORK STATUS ===
echo.
netsh wlan show hostednetwork
echo.
echo =============================

:: Extract client count
set "ClientCount=0"
for /f "tokens=2 delims=:" %%a in ('netsh wlan show hostednetwork ^| findstr /i "Number of clients"') do (
for /f "tokens=*" %%b in ("%%a") do set "ClientCount=%%b"
)

echo.
if "%ClientCount%"=="0" (
echo [INFO] No devices currently connected.
) else (
echo [INFO] %ClientCount% device(s^) connected.
echo.
echo [TIP] To see connected devices' IP addresses:
echo arp -a
)

pause
endlocal

Method 4: Modern Mobile Hotspot (Windows 10/11)​

If the legacy hostednetwork isn't supported by your Wi-Fi driver, use the modern Mobile Hotspot API through PowerShell.

@echo off
setlocal

:: Check for Administrator privileges
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] This script requires Administrator privileges.
pause
endlocal
exit /b 1
)

echo [ACTION] Configuring Mobile Hotspot via modern API...
echo.

set "Action=start"
if /i "%~1"=="stop" set "Action=stop"

if /i "%Action%"=="start" (
powershell -NoProfile -Command ^
"try { "^
" <# Check if Mobile Hotspot can be started #> "^
" $connectionProfile = [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetInternetConnectionProfile(); "^
" if (-not $connectionProfile) { "^
" Write-Host '[ERROR] No active internet connection to share.'; "^
" exit 1; "^
" }; "^
" Write-Host '[INFO] Internet connection found. Opening Mobile Hotspot settings...'; "^
" Write-Host ''; "^
" Write-Host 'The Mobile Hotspot settings page will now open.'; "^
" Write-Host 'Toggle the switch ON and configure your SSID/password.'; "^
" Write-Host ''; "^
" Start-Process 'ms-settings:network-mobilehotspot'; "^
" Write-Host '[TIP] For fully automated control, use the Windows.Networking.NetworkOperators API.'; "^
"} catch { "^
" Write-Host ('[ERROR] ' + $_.Exception.Message); "^
"}"
) else (
echo [ACTION] Opening Mobile Hotspot settings to disable...
powershell -NoProfile -Command "Start-Process 'ms-settings:network-mobilehotspot'"
)

pause
endlocal
Why open Settings instead of automating fully?

The modern Mobile Hotspot API (Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager) requires UWP app capabilities that are difficult to invoke from a Batch/PowerShell script. Opening the settings page directly is the most reliable approach for interactive use. For fully automated scenarios, consider a compiled application or a scheduled task that uses the legacy netsh method on supported hardware.

How to Avoid Common Errors​

Wrong Way: Assuming All Cards Support Hosted Networks​

Many newer Wi-Fi drivers (using the WDI driver model) have removed the Hosted Network feature in favor of the modern "Mobile Hotspot."

Correct Way: Before trying to start the hotspot, verify support:

netsh wlan show drivers | findstr /i "Hosted network supported"

If it says No, the legacy netsh method will not work. Use Method 4 or the Windows Settings toggle instead.

Wrong Way: Running Without Administrator Privileges​

Modifying wireless network configuration is a system-level change. Without elevation, the commands fail with confusing error messages.

Correct Way: Always check for elevation at the start:

net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Run as Administrator.
exit /b 1
)

Wrong Way: Using a Weak Password​

Since you are acting as the router, any device that joins your hotspot can potentially intercept traffic or access local resources. A password like 12345678 is trivially crackable.

Correct Way: Use a strong password of at least 12 characters with mixed case, numbers, and symbols. WPA2 requires a minimum of 8 characters. Method 1 validates the minimum length automatically.

Problem: Internet Sharing Not Automatic​

Creating a hotspot only creates the Wi-Fi access point. It does not automatically share your laptop's internet connection.

Solution: You must enable Internet Connection Sharing (ICS):

  1. Open ncpa.cpl
  2. Right-click your internet-connected adapter (Ethernet or primary Wi-Fi)
  3. Properties → Sharing tab
  4. Check "Allow other network users to connect"
  5. Select the new virtual adapter from the dropdown

Problem: Hotspot Stops Randomly​

The hotspot often stops when the laptop enters Sleep or Power Save mode, disconnecting all clients.

Solution: Change your Wi-Fi adapter's power management settings:

  1. Open Device Manager
  2. Find your Wi-Fi adapter → Properties → Power Management
  3. Uncheck "Allow the computer to turn off this device to save power"

Or via command line:

powershell -NoProfile -Command ^
"Get-NetAdapter -Name 'Wi-Fi' | Disable-NetAdapterPowerManagement -ErrorAction SilentlyContinue"

Best Practices and Rules​

1. Security First​

  • Use a strong password (12+ characters recommended)
  • Change the default SSID to something that doesn't reveal your device type
  • Be aware that all traffic from connected devices passes through your laptop
  • Consider using WPA3 if your hardware supports it

2. Identify Disconnections​

If the hotspot randomly stops, check:

  • Power save settings (see above)
  • Wi-Fi driver updates
  • Whether another application is taking control of the Wi-Fi adapter

3. Clear Driver Issues​

If you cannot start the network after it previously worked:

:: Reset the hosted network
netsh wlan stop hostednetwork
netsh wlan set hostednetwork mode=disallow
netsh wlan set hostednetwork mode=allow
netsh wlan start hostednetwork

If that doesn't work, try resetting the network stack:

netsh winsock reset
netsh int ip reset
:: Restart required after these commands

4. Log Hotspot Activity​

In shared environments, always log when the hotspot is started and stopped, and by whom. All methods above include automatic logging to %USERPROFILE%\hotspot_log.txt.

5. Check Connected Devices​

Periodically audit who is connected to your hotspot. An unexpected client could indicate someone has guessed your password:

netsh wlan show hostednetwork
arp -a

6. Always Use setlocal / endlocal​

Without setlocal, every variable your script creates persists in the parent shell session, causing potential conflicts when running multiple scripts in sequence.

Conclusions​

Creating an ad-hoc Wi-Fi hotspot via Batch script is a powerful way to provide local connectivity on the fly. By moving from GUI-based toggles to automated command-line control, you gain the ability to deploy predictable, branded wireless links for specific tasks. This capability is essential for field work, legacy device support, and maintaining a high level of control over your local wireless environment.