DEV Community

ZAK
ZAK

Posted on Fully Autonomous

Generate a Wi-Fi QR Code from Your Windows PC (Python + netsh)

Guests at the office, a new phone, a Raspberry Pi with a camera... at some point everyone ends up reading a 24-character Wi-Fi password out loud. A QR code fixes that: point a phone camera at it and you're connected.

Windows 11 can already show this QR code in the network properties, and if you just want the clicks, this guide walks through it (including the Windows 10 workaround): how to connect to Wi-Fi using a QR code on Windows.

But as developers, we can do better: generate the code ourselves, script it, print it, or drop it into an internal dashboard. Here's how.

The format behind every Wi-Fi QR code

A Wi-Fi QR code is just a text string. Phones (iOS and Android) recognize this pattern:

WIFI:T:WPA;S:MyNetwork;P:MyPassword;H:false;;
Enter fullscreen mode Exit fullscreen mode
Field Meaning Values
T Security type WPA (also covers WPA2/WPA3), WEP, nopass
S SSID (network name) your network name
P Password omit for nopass
H Hidden network true / false (optional)

The string ends with a double semicolon ;;.

The gotcha: the characters \ ; , : " must be escaped with a backslash. A password like pa;ss:word becomes pa\;ss\:word. Forget this and the phone will happily join... nothing.

Getting the SSID and password with netsh

Windows stores every network you've joined as a profile. Two netsh commands give you everything:

# Name of the network you're connected to right now
netsh wlan show interfaces

# Saved password for a profile (needs to be a network you've joined)
netsh wlan show profile name="MyNetwork" key=clear
Enter fullscreen mode Exit fullscreen mode

Look for the SSID line in the first output and Key Content in the second.

⚠️ netsh output is localized. On a French Windows, Key Content is Contenu de la clé. Adjust the regex below if your system isn't in English.

The script

Install the QR library (Pillow is used to write the PNG):

pip install "qrcode[pil]"
Enter fullscreen mode Exit fullscreen mode

Then:

import re
import subprocess
import qrcode


def run(cmd: str) -> str:
    return subprocess.run(cmd, capture_output=True, text=True, shell=True).stdout


def current_ssid() -> str | None:
    # ^\s*SSID avoids matching the BSSID line
    m = re.search(r"^\s*SSID\s*:\s*(.+)$", run("netsh wlan show interfaces"), re.M)
    return m.group(1).strip() if m else None


def saved_password(ssid: str) -> str:
    out = run(f'netsh wlan show profile name="{ssid}" key=clear')
    m = re.search(r"Key Content\s*:\s*(.+)", out)
    return m.group(1).strip() if m else ""


def escape(value: str) -> str:
    return re.sub(r'([\\;,:"])', r"\\\1", value)


ssid = current_ssid()
if not ssid:
    raise SystemExit("Not connected to a Wi-Fi network.")

password = saved_password(ssid)
security = "WPA" if password else "nopass"

payload = f"WIFI:T:{security};S:{escape(ssid)};"
if password:
    payload += f"P:{escape(password)};"
payload += ";"

qrcode.make(payload).save("wifi.png")
print(f"QR code for '{ssid}' saved to wifi.png")
Enter fullscreen mode Exit fullscreen mode

Run it, open wifi.png, scan it with your phone. Done.

A few things worth knowing

  • It's a password in plain text. Anyone who scans (or photographs) the code can decode the string. Don't post it in a public channel, and treat a printed copy like a sticky note with the password on it.
  • Change the password, regenerate the code. Old codes don't update themselves.
  • Won't scan? Usually it's screen brightness, glare, or holding the phone too close. 15 to 20 cm from the screen works well.
  • PCs can't really "scan to join". Phones do this natively; on Windows, a webcam QR reader is hit-and-miss. The realistic flow is: scan with your phone, read the password, type it on the PC.

Ideas to take it further

  • Wrap it in a small Flask page for your office "guest Wi-Fi" screen.
  • Loop over netsh wlan show profiles to generate a code for every saved network (useful when migrating to a new laptop).
  • Add the SSID as a caption under the QR code with Pillow, so the printout explains itself.

Further reading

Have you automated something similar? I'd love to see other approaches in the comments 👇

Top comments (0)