Skip to main content

How to Export and Import Wi-Fi Profiles in Batch Script

Managing Wi-Fi connections across multiple Windows devices can be a tedious manual task. Whether you are setting up a new fleet of laptops for a small office or simply backing up your home network settings, automating the export and import of Wi-Fi profiles is a major time-saver. By using Batch scripts and the built-in netsh utility, you can migrate your saved wireless networks in seconds.

This guide will explain how to use the netsh wlan command to handle wireless profiles programmatically, ensuring your configurations remain consistent and secure.

The Core Command: Netsh WLAN

Windows stores Wi-Fi profiles as XML files internally. The netsh (Network Shell) utility provides a dedicated context for wireless LAN settings (wlan) that allows you to interact with these profiles, listing, exporting, and importing them.

How to View Available Wi-Fi Profiles

Before exporting, you may want to see which profiles are currently stored on your machine.

@echo off
setlocal

rem --- Verify the Wireless AutoConfig service is running ---
sc query WlanSvc 2>nul | findstr /i /c:"RUNNING" >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] The Wireless AutoConfig service is not running.
echo [INFO] This machine may not have a Wi-Fi adapter.
endlocal
pause
exit /b 1
)

echo [INFO] Saved Wi-Fi profiles on this machine:
echo.
netsh wlan show profiles

endlocal
pause

This lists all SSIDs (Service Set Identifiers) that your computer has previously connected to and saved.

Method 1: Exporting All Wi-Fi Profiles

The most efficient way to back up every saved network is to use the export profile command without specifying a name, which exports all profiles into a target folder.

@echo off
setlocal

set "BackupDir=%~dp0WiFiBackups"

rem --- Admin check: exporting with key=clear requires elevation ---
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] This script must be run as Administrator.
echo [INFO] Exporting passwords in clear text requires elevation.
endlocal
pause
exit /b 1
)

rem --- Verify the Wireless AutoConfig service is running ---
sc query WlanSvc 2>nul | findstr /i /c:"RUNNING" >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Wireless AutoConfig service is not running.
endlocal
pause
exit /b 1
)

rem --- Create the backup folder if it does not exist ---
if not exist "%BackupDir%" (
mkdir "%BackupDir%"
if %errorlevel% neq 0 (
echo [ERROR] Failed to create backup directory: %BackupDir%
endlocal
pause
exit /b 1
)
)

echo [EXPORT] Exporting all Wi-Fi profiles to:
echo %BackupDir%
echo.

rem --- Export all profiles with passwords in clear text ---
netsh wlan export profile folder="%BackupDir%" key=clear

if %errorlevel% neq 0 (
echo.
echo [ERROR] Export command failed.
endlocal
pause
exit /b 1
)

rem --- Count how many XML files were created ---
set "FileCount=0"
for %%f in ("%BackupDir%\*.xml") do set /a FileCount+=1

echo.
if %FileCount% equ 0 (
echo [WARN] No profiles were exported. There may be no saved Wi-Fi networks.
) else (
echo [SUCCESS] %FileCount% profile(s^) exported to %BackupDir%
)

endlocal
pause

Explaining the Parameters

  • folder="%BackupDir%": Specifies the directory where the XML files will be saved. Each profile is saved as a separate file named like Wi-Fi-ProfileName.xml.
  • key=clear: By default, Windows exports the Wi-Fi password in an encrypted format that only works on the same machine. Adding key=clear saves the password in plain text within the XML, making it possible to import onto a different machine and have it work immediately.
warning

Security Warning: Because key=clear saves passwords in plain text, the resulting XML files are sensitive data. Anyone who can read these files has your Wi-Fi passwords. Never leave them on a shared drive, in unencrypted cloud storage, or on an unattended USB stick. Delete the files after importing them onto the target machines.

Method 2: Exporting a Single Specific Profile

If you only need to back up one network rather than all of them, specify the profile name.

@echo off
setlocal

set "ProfileName=Office_Internal_WiFi"
set "BackupDir=%~dp0WiFiBackups"

rem --- Admin check ---
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] This script must be run as Administrator.
endlocal
pause
exit /b 1
)

rem --- Verify the profile exists ---
netsh wlan show profiles 2>nul | findstr /i /c:"%ProfileName%" >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Profile "%ProfileName%" not found.
echo [INFO] Available profiles:
netsh wlan show profiles
endlocal
pause
exit /b 1
)

rem --- Create the backup folder ---
if not exist "%BackupDir%" mkdir "%BackupDir%"

echo [EXPORT] Exporting profile "%ProfileName%"...

netsh wlan export profile name="%ProfileName%" folder="%BackupDir%" key=clear

if %errorlevel% equ 0 (
echo [SUCCESS] Profile exported to %BackupDir%
) else (
echo [ERROR] Export failed for "%ProfileName%".
)

endlocal
pause

Method 3: Importing Wi-Fi Profiles

Once you have your XML files, you can import them onto another computer. This is particularly useful for technicians setting up new workstations or deploying standardized configurations.

Script to Import All XML Files from a Folder

@echo off
setlocal enabledelayedexpansion

set "SourceDir=%~dp0WiFiBackups"

rem --- Admin check: importing profiles modifies system-level settings ---
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] This script must be run as Administrator.
echo [INFO] Importing Wi-Fi profiles requires elevation.
endlocal
pause
exit /b 1
)

rem --- Verify the Wireless AutoConfig service is running ---
sc query WlanSvc 2>nul | findstr /i /c:"RUNNING" >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Wireless AutoConfig service is not running.
endlocal
pause
exit /b 1
)

rem --- Verify the source directory exists ---
if not exist "%SourceDir%" (
echo [ERROR] Source directory not found: %SourceDir%
endlocal
pause
exit /b 1
)

rem --- Check if there are any XML files to import ---
set "HasFiles=0"
for %%f in ("%SourceDir%\*.xml") do set "HasFiles=1"

if "!HasFiles!"=="0" (
echo [ERROR] No XML files found in %SourceDir%
endlocal
pause
exit /b 1
)

echo [IMPORT] Importing Wi-Fi profiles from:
echo %SourceDir%
echo.

set "SuccessCount=0"
set "FailCount=0"

for %%f in ("%SourceDir%\*.xml") do (
echo Importing: %%~nxf
netsh wlan add profile filename="%%f" user=all >nul 2>&1

if !errorlevel! equ 0 (
echo [OK] Imported successfully.
set /a SuccessCount+=1
) else (
echo [FAIL] Could not import. File may be corrupt or invalid.
set /a FailCount+=1
)
)

echo.
echo ============================================
echo Results: !SuccessCount! imported, !FailCount! failed
echo ============================================

if !FailCount! gtr 0 (
echo [WARN] Some profiles failed to import. Common causes:
echo - The XML file was manually edited and has formatting errors.
echo - The profile already exists with different settings.
echo - The XML was exported from a newer version of Windows.
)

endlocal
pause

Explaining the Import Logic

  1. for %%f in ("%SourceDir%\*.xml"): This loop processes every .xml file in the backup directory.
  2. netsh wlan add profile: Registers the XML profile into the Windows wireless database.
  3. user=all: Makes the profile available to every user account on the machine, not just the one running the script. Use user=current to restrict it to the current account only.

User Scope Options

ParameterResult
user=allProfile is shared across all Windows accounts on the PC. Requires Administrator.
user=currentProfile is only visible and usable by the current logged-in account.

Method 4: One-Click USB Migration Tool

This practical script is designed to live on a USB drive alongside a WiFi_Config folder. Plug the USB into any new machine, run the script as Administrator, and all profiles are imported automatically.

@echo off
setlocal enabledelayedexpansion

rem --- Work from the script's own directory (USB drive root) ---
set "ConfigDir=%~dp0WiFi_Config"

echo ============================================
echo Wi-Fi Profile Migration Tool
echo ============================================
echo.

rem --- Admin check ---
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Right-click this script and select "Run as Administrator".
endlocal
pause
exit /b 1
)

rem --- Verify the Wireless AutoConfig service ---
sc query WlanSvc 2>nul | findstr /i /c:"RUNNING" >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Wireless AutoConfig service is not running.
echo [INFO] Start it with: net start WlanSvc
endlocal
pause
exit /b 1
)

rem --- Check for the config folder ---
if not exist "%ConfigDir%" (
echo [ERROR] Folder not found: %ConfigDir%
echo [INFO] Create a folder named "WiFi_Config" next to this script
echo and place your exported XML files inside it.
endlocal
pause
exit /b 1
)

rem --- Check for XML files ---
set "HasFiles=0"
for %%x in ("%ConfigDir%\*.xml") do set "HasFiles=1"

if "!HasFiles!"=="0" (
echo [ERROR] No XML files found in %ConfigDir%
endlocal
pause
exit /b 1
)

rem --- Import each profile ---
echo [INFO] Importing profiles from: %ConfigDir%
echo.

set "SuccessCount=0"
set "FailCount=0"

for %%x in ("%ConfigDir%\*.xml") do (
echo Importing: %%~nxf
netsh wlan add profile filename="%%x" user=all >nul 2>&1

if !errorlevel! equ 0 (
echo [OK]
set /a SuccessCount+=1
) else (
echo [FAIL]
set /a FailCount+=1
)
)

echo.
echo ============================================
echo Done: !SuccessCount! imported, !FailCount! failed
echo ============================================
echo.

rem --- Show the resulting profile list ---
echo [INFO] Profiles now available on this machine:
netsh wlan show profiles

endlocal
pause

How to Avoid Common Errors

Wrong Way: Exporting Without key=clear for Cross-Machine Use

By default, netsh wlan export profile encrypts the password using the local machine's credentials. The exported XML works fine on the same computer, but importing it onto a different machine results in a profile with no usable password, the connection will fail silently.

rem *** BAD for cross-machine migration - password is encrypted to this PC ***
netsh wlan export profile folder="C:\Backup"

Correct Way: Always use key=clear when you intend to import on other machines.

rem *** GOOD: password stored in plain text, portable to any PC ***
netsh wlan export profile folder="C:\Backup" key=clear

Wrong Way: Manually Editing XML Profile Files

It might be tempting to open an exported XML file and change the SSID or password directly. While the XML structure looks simple, Wi-Fi profiles contain interdependent fields (hex-encoded SSIDs, key type indicators, authentication parameters) that must be consistent. Manual edits often produce files that import without error but fail to connect.

Error you will see: The profile 'WiFi-Name' is not added. Error: The profile is invalid.

Correct Way: To create a profile for a new network, connect to it manually once on any machine, export the verified profile, and then distribute that XML.

Wrong Way: Running Import Without Administrator

The netsh wlan add profile command modifies system-level network settings and requires elevation. Without it, the import fails with an access-denied error.

rem *** BAD: fails without elevation ***
netsh wlan add profile filename="profile.xml" user=all

Correct Way: Always check for admin rights at the start of the script.

Problem: Profile Already Exists

If a profile with the same name already exists on the target machine, netsh wlan add profile may fail or overwrite it depending on the Windows version. The behavior is not consistent.

Solution: To force an update, delete the existing profile first, then import.

rem --- Remove existing profile before importing ---
netsh wlan delete profile name="Office_WiFi" >nul 2>&1
netsh wlan add profile filename="Wi-Fi-Office_WiFi.xml" user=all

Best Practices and Rules

1. Treat Exported XML Files as Passwords

When exported with key=clear, these files contain your Wi-Fi passwords in plain text. Store them with the same care you would give a password list, encrypted storage, limited access, and deletion after use.

2. Always Use key=clear for Migration

If you forget key=clear during export, the profile XML will contain an encrypted key blob that only works on the original computer. The import will succeed on the new machine, but the connection will fail because the password cannot be decrypted.

3. Verify After Import

After importing profiles, run netsh wlan show profiles to confirm they appear in the list. Then test an actual connection with netsh wlan connect name="ProfileName" to verify the credentials work.

4. Use user=all for Shared Machines

On machines used by multiple people (kiosks, shared workstations, lab computers), use user=all so every account can access the Wi-Fi profile. Use user=current on personal machines where you want to restrict access.

5. Use setlocal / endlocal

Always wrap scripts in setlocal and endlocal to prevent variables from leaking into the calling environment.

6. The netsh Parameters Are Language-Independent

The netsh wlan command parameters (export, add, profile, folder, key, user, filename) are the same in every Windows display language. Only the descriptive output text is translated. This makes these scripts inherently multilanguage-safe.

Conclusions

Exporting and importing Wi-Fi profiles via Batch script is a powerful technique for system administrators and power users alike. By leveraging netsh wlan export with key=clear and netsh wlan add profile with user=all, you bypass the manual "connect and enter password" workflow completely. The critical points are: always export with key=clear for cross-machine portability, always run import scripts as Administrator, always verify the import succeeded by checking the profile list, and always treat the exported XML files as sensitive data. With proper error handling and validation, these scripts become reliable tools for fleet deployment, workstation setup, and configuration backup.