Skip to content

Instantly share code, notes, and snippets.

@p0w3rsh3ll
Created June 29, 2023 11:23
Show Gist options
  • Save p0w3rsh3ll/2e2da2091e3669cc89803b74c0d7aea7 to your computer and use it in GitHub Desktop.
Save p0w3rsh3ll/2e2da2091e3669cc89803b74c0d7aea7 to your computer and use it in GitHub Desktop.
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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment