Get a list of Wifi networks with PowerShell
Returns the parsed output of netsh wlan show network mode=bssid in psobject form. Does exactly what it says on the tin. Requires Vista/2008 or higher, or XP SP3 with a hotfix (I can't recall which one, sorry.)
function Get-WifiNetwork {
end {
netsh wlan sh net mode=bssid | % -process {
if ($_ -match '^SSID (\d+) : (.*)$') {
$current = @{}
$networks += $current
$current.Index = $matches[1].trim()
$current.SSID = $matches[2].trim()
} else {
if ($_ -match '^\s+(.*)\s+:\s+(.*)\s*$') {
$current[$matches[1].trim()] = $matches[2].trim()
}
}
} -begin { $networks = @() } -end { $networks|% { new-object psobject -property $_ } }
}
}
ps> Get-WifiNetwork| select index, ssid, signal, 'radio type' | sort signal -desc | ft -auto
Loading Comments ...
Comments
AnonymousCoward posted this comment on 2020-10-16:
#[CmdletBinding()] Param ([Parameter(Mandatory=$true)][ValidateSet('Off', 'On')][string]$WifiStatus)
# https://www.reddit.com/r/sysadmin/comments/9az53e/need_help_controlling_wifi/
$WifiStatus = "On"
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? {$_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1'})[0]
Function Await($WinRtTask, $ResultType)
{
$asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
$netTask = $asTask.Invoke($null, @($WinRtTask))
$netTask.Wait(-1) | Out-Null
$netTask.Result
}
[Windows.Devices.Radios.Radio,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
[Windows.Devices.Radios.RadioAccessStatus,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
Await ([Windows.Devices.Radios.Radio]::RequestAccessAsync()) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null
$radios = Await ([Windows.Devices.Radios.Radio]::GetRadiosAsync())([System.Collections.Generic.IReadOnlyList[Windows.Devices.Radios.Radio]])
$wifi = $radios | ? { $_.Kind -eq 'WiFi' }
[Windows.Devices.Radios.RadioState,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
# necessary since Windows will automatically enable the status of a wifi device when the wired connection is lost
# Start-Sleep -Seconds 10
# Await ($wifi.SetStateAsync($WifiStatus)) ([Windows.Devices.Radios.RadioAccessStatus])
if ($wifi.State -eq "On") {
[Windows.Devices.WiFi.WiFiAdapter,Windows.System.Devices,Contenttype=WindowsRuntime] | Out-Null
$Res=Await ([Windows.Devices.WiFi.WiFiAdapter]::FindAllAdaptersAsync())([System.Collections.Generic.IReadOnlyList[Windows.Devices.WiFi.WiFiAdapter]])
$res.NetworkReport.AvailableNetworks | Select Ssid -Unique
}
this is powershell !
dwirch posted this comment on 2020-10-16:
Good comment! You should login and write a post explaining how all this works.
For comparison, for those who are confused,
One method shells to NetSh, the other method leverages .Net assemblies.
NetSH is a pre-compiled executable that comes with every Windows OS. .Net is a set of pre-compiled libraries and assemblies that comes with every modern version of the Windows OS.
One method simply string-parses the output of another program. The other parses information given back from the execution of a .Net function.
Anyone else got a favorite method of gathering WiFi networks?
You must be logged in to make a comment.
AnonymousCoward posted this comment on 2020-10-16:
This is not poweshell !!!!!!