Get-SavedWifiNetworks.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<#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)"
}