Test if the connection is metered

I’ve been looking to some PowerShell script written by Microsoft to diagnose connectivity issues with Microsoft Defender for Endpoint (MDE) and found a little gem in the code:

I’ve written a function for that:

Function Test-isUsingMeteredConnection {
<#
.SYNOPSIS
Test if a metered connection is in use
.DESCRIPTION
Test if a metered connection is in use
.EXAMPLE
Test-isUsingMeteredConnection
False
#>
[OutputType([System.Boolean])]
[CmdletBinding()]
Param()
Begin {
try {
$null = [Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType = WindowsRuntime]
$connectionProfile = [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile()
} catch {
Throw $_
}
}
Process {}
End {
if ($connectionProfile) {
$cost = $connectionProfile.GetConnectionCost()
if ($cost.ApproachingDataLimit -or $cost.OverDataLimit -or
$cost.Roaming -or $cost.BackgroundDataUsageRestricted -or
(
($cost.NetworkCostType -ne [Windows.Networking.Connectivity.NetworkCostType]::Unrestricted.value__) -and
($cost.NetworkCostType -ne [Windows.Networking.Connectivity.NetworkCostType]::Unknown.value__)
)
) {
return $true
} else {
return $false
}
} else {
$false
}
}
}
Export-ModuleMember -Function 'Test-isUsingMeteredConnection'
view raw Metered.psm1 hosted with ❤ by GitHub

This .Net object is very promising and has many methods compared to what you get when use the built-in Get-NetConnectionProfile cmdlet that uses WMI.

You can for example find how many signal bars you’ve on your Wireless card:

$null = [Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType = WindowsRuntime]
$connectionProfile = [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile()       $connectionProfile.GetSignalBars()

Awesome isn’t?

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.