Get-SavedWifiNetworks.ps1

<#PSScriptInfo
 
.VERSION 1.1
 
.GUID ec9f3b67-428a-4839-92ef-4860df1de836
 
.AUTHOR Aaron Guilmette
 
.COMPANYNAME Microsoft
 
.COPYRIGHT 2022
 
.TAGS Export Wifi Passwords
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
.DESCRIPTION
Export SSID profiles and passwords.
 
.PRIVATEDATA
 
#>

param (
    [switch]$AllProfiles,
    [string]$Output = (Get-Date -Format yyyy-MM-dd)+"_AllWifiPasswords.csv",
    [string]$WifiProfile,
    [switch]$ShowProfiles
)

# Check if Elevated
$wid = [system.security.principal.windowsidentity]::GetCurrent()
$prp = New-Object System.Security.Principal.WindowsPrincipal($wid)
$adm = [System.Security.Principal.WindowsBuiltInRole]::Administrator
if ($prp.IsInRole($adm))
{
    Write-Host -ForegroundColor Green "Elevated PowerShell session detected. Continuing."
}
else
{
    Write-Host -ForegroundColor Red "This application/script must be run in an elevated PowerShell window. Please launch an elevated session and try again."
    Break
}

function ChooseProfile
{
    # Get Profiles and build menu
    [array]$AllWifiSSIDs = (netsh.exe wlan show profiles)
    [array]$AllWifiSSIDs = $AllWifiSSIDs | % { ($_.Split(":")[1]) }
    
    $ProfileMenu = @{ }
    $Choice = $null
    Write-Host -Fore Yellow "Available WLAN Profiles"
    For ($i = 1; $i -le $AllWifiSSIDs.Count; $i++)
    {
        Write-Host "$i. $($AllWifiSSIDs[$i - 1])"
        $ProfileMenu.Add($i, ($AllWifiSSIDs[$i - 1]))
    }
    
    If (!($WifiProfile))
    {
        [int]$script:ProfileSelection = Read-Host "Enter SSID/Network profile number"
        $ProfileName = $ProfileMenu.Item($ProfileSelection)
    }
    
    Write-Host -NoNewline "Profile selected: "; Write-Host -ForegroundColor Green "$($ProfileName)"
    
    $Choice = Read-Host "Correct? [Y/N]"
    [array]$script:WifiProfile = $ProfileName
    
    while ($Choice -ne "Y")
    {
        If ($WifiProfile) { Remove-Variable WifiProfile -ea SilentlyContinue }
        ChooseProfile
    }
}

If (!$WiFiProfile -and !$AllProfiles)
{
    ChooseProfile
}

If ($WiFiProfile) { [array]$AllSSIDs = $WiFiProfile }
If ($AllProfiles)
{
    [array]$AllSSIDs = (netsh.exe wlan show profiles)
    [array]$AllSSIDs = $AllSSIDs | % { ($_.Split(":")[1]) }
}

$global:AllSSIDPasswords = @()
foreach ($obj in $AllSSIDs)
{
    if ($obj -ne $null -and $obj -ne " " -and $obj -ne "")
    {
        $KeyContent = $null
        $PasswordValue = $null
        $objProfileName = $obj.Trim()
        $Temp = (netsh.exe wlan show profiles name="$($objProfileName)" key=clear)
        $KeyContent = $Temp | Select-String -Pattern "Key Content"
        If ($KeyContent) { $PasswordValue = $KeyContent.ToString().Split(":")[1].Trim() }
        If (!($PasswordValue)) { $PasswordValue = "Not present" }
        $Global:Record = [PSCustomObject]@{
            ProfileName = "$($objProfileName)"
            Password    = "$($PasswordValue)"
        }
        Try { $global:AllSSIDPasswords += $global:Record }
        Catch { }
    }
}
$AllSSIDPasswords | Export-Csv $Output -NoTypeInformation -Force
Write-Host "All SSID/Profiles and corresponding passwords have been exported if available. The results are available in this session with the `$AllSSIDPasswords, which you can pipe to destinations such as console, Out-GridView, or Export-Csv."

If ($WiFiProfile -and $PasswordValue)
{
    Write-Host -NoNewLine " Profile Selection #: "
    Write-Host -ForegroundColor Green $($ProfileSelection)
    Write-Host -NoNewLine " Profile / SSID Name: "
    Write-Host -ForegroundColor Green $WifiProfile.Trim()
    Write-Host -NoNewline " Password: "
    Write-Host -ForegroundColor Green "$($PasswordValue)"
}