Functions/Get-WifiNetworks.ps1


function Get-WifiNetworks {
    [CmdletBinding()]
    param (
        [Parameter()] [switch] $SaveToOneDrive
    )


    $result = @()

    $wifi = $(netsh.exe wlan show profiles)

    if ($wifi -match "There is no wireless interface on the system.") {
        Write-Output $wifi
        exit
    }

    $ListOfSSID = ($wifi | Select-String -Pattern "\w*All User Profile.*: (.*)" -AllMatches).Matches | ForEach-Object { $_.Groups[1].Value }
    # $NumberOfWifi = $ListOfSSID.count
    # Write-Warning "[$(Get-Date)] I've found $NumberOfWifi Wi-Fi Connection settings stored in your system $($env:computername) : "
    foreach ($SSID in $ListOfSSID) {
        try {
            $passphrase1 = ($(netsh.exe wlan show profiles name="$SSID" key=clear) | Select-String -Pattern ".*Key Content.*:(.*)" -AllMatches).Matches.Value
            Write-Verbose $passphrase1
            $passphrase = ($passphrase1 -split ":")[1]
            Write-Verbose $passphrase
        } catch {
            $passphrase = "N/A"
        }
        $obj = [PSCustomObject]@{
            Name       = ($SSID).Trim()
            PassPhrase = ($passphrase).Trim()
        }
        $result += $obj
    }

    if ($SaveToOneDrive) {
        $OutFile = "$($env:OneDrive)\WiFi_$($env:COMPUTERNAME).csv"
        Remove-Item $OutFile -Force -ea 0
        $result | Export-Csv $OutFile -NoClobber -Force -Verbose
    }

    return $result

}