DEV Community

Cover image for Easily retrieve windows Wi-Fi passwords from the command line
Adam DeHaven
Adam DeHaven

Posted on • Updated on • Originally published at adamdehaven.com

Easily retrieve windows Wi-Fi passwords from the command line

The Typical Offsite Meeting

We've all been there. You just arrived at an offsite meeting. The client directs you to the conference room where you'll be spending the better part of your day. Laid out before you, as part of their welcome spread, is a platter of bagels and muffins, luke-warm coffee, and on the whiteboard, the guest Wi-Fi password.

After spending the first few minutes setting up your impromptu work station, you connect to the guest Wi-Fi network in order to get your client presentation ready. The client erases the whiteboard that doubles as the screen for the projector.

Enter your late co-worker. πŸ˜’

Right as you're about to start presenting, your co-worker is frantically trying to connect to all the available Wi-Fi networks. You want to help, but you don't remember the random sports team that was in the password that has since been erased from the board. Was the password go_R@MS_123 or g0_RAMS_123?

We're going to create a script to run the next time this happens that will easily output the saved Wi-Fi network's password so that you can discretely save your co-worker (again).

Wi-Fi Password Retrieval Script

To get started, open your favorite text editor, create a new file, and paste in the entire script shown below.

#!/bin/sh

# -----------  SET COLORS  -----------
COLOR_RED=$'\e[31m'
COLOR_CYAN=$'\e[36m'
COLOR_YELLOW=$'\e[93m'
COLOR_GREEN=$'\e[32m'
COLOR_RESET=$'\e[0m'

#
# Helper Function to get stored Wi-Fi network SSIDs
# --------------------------------------------------------------------------
getwifinetworks() {
    echo "#    "
    echo "#    Saved Network SSIDs"
    echo "#    -------------------"
    netsh wlan show profiles | findstr All | sed "s/    All User Profile     : /${COLOR_RESET}#    ${COLOR_CYAN}/"
    echo "${COLOR_RESET}#    -------------------"
    echo "#    "
}

#
# Get password of stored Wi-Fi network by SSID.
# --------------------------------------------------------------------------
# Display list of stored SSIDs
getwifinetworks

# Prompt user for Wi-Fi SSID
echo "#    Enter the network name (SSID)"
echo "#    from the list above: "
echo "#    "
read -e -p "#    SSID: ${COLOR_CYAN}" SSID
eval SSID=$SSID

# If SSID is empty, exit early
if [ -z "$SSID" ]; then
    echo "${COLOR_RESET}#    "
    echo "#    ${COLOR_RED}An SSID is required. Please try again.${COLOR_RESET}"
else
    pass=$(netsh wlan show profile name="$SSID" key=clear | findstr Key)

    # If password is empty, notify user they may need
    # elevated privileges
    if [ -z "$pass" ]; then
        echo "${COLOR_RESET}#    "
        echo "#    ${COLOR_RED}If the network SSID appeared in the list above,${COLOR_RESET}"
        echo "#    ${COLOR_RED}fetching the password may require elevated privileges.${COLOR_RESET}"
        echo "#    "
        echo "#    ${COLOR_YELLOW}Open a new prompt with administrator rights${COLOR_RESET}"
        echo "#    ${COLOR_YELLOW}and try running the 'getwifipassword' command again.${COLOR_RESET}"
        echo "#    ${COLOR_YELLOW}Otherwise, the password is not stored on this device.${COLOR_RESET}"
    else
        echo "${COLOR_RESET}#    "
        echo "#    Password for '${SSID}':"
        echo "#    -----------------------------"
        echo "#   " $pass | sed "s/Key Content : /${COLOR_GREEN}/"
        echo "${COLOR_RESET}#    -----------------------------"
    fi
fi
Enter fullscreen mode Exit fullscreen mode

Save the file as getwifipassword.sh. You will want to save the script in a location that you can easily access from the command line. Alternatively, you can save the file at the location of your choosing and create an alias.

If you're lazy, you can download the script directly from GitHub.

GitHub logo adamdehaven / get-wifi-password

Simple shell script to grab saved wifi passwords on Windows

Usage

To launch the interactive prompt, simply open Command Prompt (or Git Bash, or whatever your terminal of choice happens to be) and call the script (or use the alias).

# Command Prompt
C:\Users\MartyMcfly>bash /path/to/getwifipassword.sh

# ====
#  OR
# ====

# Git Bash
$ bash /path/to/getwifipassword.sh
Enter fullscreen mode Exit fullscreen mode

When the prompt loads, you will be shown a list of saved network SSIDs.

Note: The SSID list may not include all of saved networks depending on the security settings on your computer.

Enter the SSID you would like to retrive the password for by typing the SSID name exactly as it appears in the list, then press Enter.

#
#    Saved Network SSIDs
#    -------------------
#    CompanyGuestPortal
#    THEROUTER
#    Home_Network
#    -------------------
#
#    Enter the network name (SSID)
#    from the list above:
#
#    SSID:
Enter fullscreen mode Exit fullscreen mode

The prompt will output the stored network password, if it exists. πŸŽ‰

Note: Some Wi-Fi passwords will require elevated privileges to access. Open a new prompt with administrator rights and try running the command again.

Create a Bash Alias

If you use Bash, you may set up an alias to call the script from anywhere instead of having to navigate to the directory where you saved the script.

To create the alias, open your .bashrc file (or create one if it does not already exist with touch ~/.bashrc). Copy and paste in the function below. Then, to call the script from anywhere, simply enter getwifipassword on your command line.

# .bashrc

# ... previous lines ...

function getwifipassword() {
  /path/to/getwifipassword.sh;
}
alias getwifipassword=getwifipassword
Enter fullscreen mode Exit fullscreen mode

Top comments (0)