Skip to content

Commit 81ccda0

Browse files
committed
♻️ refactor: cleaner code
1 parent 376f25e commit 81ccda0

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function Get-WifiPassword {
2+
<#
3+
.SYNOPSIS
4+
Function to get wifi password
5+
.DESCRIPTION
6+
Prints password to the console
7+
.PARAMETER WifiName
8+
Determine the name of the Wi-Fi
9+
.EXAMPLE
10+
Get-WifiPassword
11+
.EXAMPLE
12+
Get-WifiPassword -name WIFINAME
13+
#>
14+
15+
[CmdletBinding()]
16+
param ([Alias('n', 'name')][string]$WifiName)
17+
18+
if (!$WifiName) {
19+
$wifiList = netsh wlan show profiles | Select-String -Pattern "All User Profile\s+:\s+(.*)" | ForEach-Object { $_.Matches.Groups[1].Value }
20+
21+
if (Get-Command gum -ErrorAction SilentlyContinue) {
22+
$WifiName = gum choose --header="Choose an available Wi-Fi name:" $wifiList
23+
}
24+
25+
elseif (Get-Command fzf -ErrorAction SilentlyContinue) {
26+
$WifiName = $wifiList | fzf --prompt="Select Wi-Fi > " --height=~80% --layout=reverse --border --exit-0 --cycle --margin="2,40" --padding=1
27+
}
28+
29+
else {
30+
for ($i = 0; $i -lt $wifiList.Count; $i++) {
31+
Write-Host "[$i] $($wifiList[$i])"
32+
}
33+
$index = $(Write-Host "Enter the corresponding number of Wi-Fi name: " -ForegroundColor Magenta -NoNewline; Read-Host)
34+
if ($null -ne $index) {
35+
if ($index -match '^\d+$' -and [int]$index -lt $wifiList.Count) {
36+
$WifiName = $wifiList[$index]
37+
} else { return }
38+
}
39+
}
40+
}
41+
42+
$WifiPassword = netsh wlan show profile name="$WifiName" key=clear | Select-String -Pattern "Key Content\s+:\s+(.+)" | ForEach-Object { $_.Matches.Groups[1].Value }
43+
44+
if (!$WifiPassword) { Write-Warning "No password available for Wi-Fi $WifiName"; return }
45+
else {
46+
Write-Verbose "Prints password for Wi-Fi $WifiName"
47+
Write-Output $WifiPassword
48+
}
49+
}
50+
51+
Set-Alias -Name 'wifipass' -Value 'Get-WifiPassword'

0 commit comments

Comments
 (0)