Thursday, November 15, 2018

Set ON/OFF Metered Ethernet Connection on windows 10

For old Windows 10 version you can change ethernet metered connection using registry
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\DefaultMediaCost
(example at https://4sysops.com/archives/set-windows-10-ethernet-connection-to-metered-with-powershell/ )

But since each Ethernet Metered Connection have it own information (since Windows 10 version 1709 I believe, in Settings / Network & Internet / Ethernet / then select your connection), there is a NEW key to know

Where is Network Card connection metered information:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DusmSvc\Profiles\{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}\* in REG_DWORD name UserCost indicate if metered or not
with {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} your Network Card GUID
and
UserCost = 0 when Metered is off
UserCost = 2 when Metered is on


Below when metered is On






























You need to restart "Data Usage" Service DusmSvc before your settings take effect



























In registry Network Card ID {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}is in:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\Y\{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}

With Y= a number



In Powershell to have GUID Network Card
Get-NetAdapter | select InterfaceDescription, InterfaceGuid
or in old Powershell version
Get-WmiObject  Win32_NetworkAdapterConfiguration -filter 'IPEnabled=True' | %{$_.GetRelated('Win32_NetworkAdapter')}| select Description,GUID




















Using this Script SetMeteredEthernetConnection.ps1 you can do it by yourself

PS C:\temp>
.\SetMeteredEthernetConnection.ps1 -MeteredAction DisplayNetworkCardOnly
Display Network Card: 'Intel(R) 82579LM Gigabit Network Connection' ID:'{2FD6004A-2CB4-4B1E-ACEB-CC5792236B44}'
PS C:\temp> .\SetMeteredEthernetConnection.ps1 -MeteredAction DisplayNetworkCardOnly -NetWorkCardLike giga
Display Network Card: 'Intel(R) 82579LM Gigabit Network Connection' ID:'{2FD6004A-2CB4-4B1E-ACEB-CC5792236B44}'
PS C:\temp> .\SetMeteredEthernetConnection.ps1 -MeteredAction DisplayNetworkCardOnly -NetWorkCardLike NOOO
PS C:\temp> .\SetMeteredEthernetConnection.ps1 -MeteredAction MeteredOff
Set MeteredOff to connection Intel(R) 82579LM Gigabit Network Connection
PS C:\temp>




#
# SetMeteredEthernetConnection.ps1
#
# by RICHARD Franck
# 2018-11
#
Param(           
     [parameter(Mandatory=$true,Position=0)][ValidateSet("DisplayNetworkCardOnly","MeteredOn","MeteredOff")][string]$MeteredAction
    ,[string]$NetWorkCardLike=""
)

If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
    Write-Warning "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!"
    Exit
}


if ($MeteredAction -eq "MeteredOn") { $MeteredChoice = 2 } else { $MeteredChoice = 0 }
if ($NetWorkCardLike -eq "") {
    $NetCards = Get-NetAdapter | select InterfaceDescription, InterfaceGuid
} else {
    $NetCards = Get-NetAdapter | Where-Object { $_.InterfaceDescription -Like "*$($NetWorkCardLike)*" } | select InterfaceDescription, InterfaceGuid
}
$RegistryName = "UserCost"
Foreach ($card in $NetCards) {
        if ($MeteredAction -eq "DisplayNetworkCardOnly") {
            Write-Host "Display Network Card: '$($card.InterfaceDescription)' ID:'$($card.InterfaceGuid)'"

        } else {
            Write-Host "Set $($MeteredAction) to connection $($card.InterfaceDescription)"
            $registryPath = "HKLM:\SOFTWARE\Microsoft\DusmSvc\Profiles\$($card.InterfaceGuid)\*"
            New-ItemProperty -Path $registryPath -Name $RegistryName -Value $MeteredChoice -PropertyType DWORD -Force | Out-Null
        }
}
# your settings take effect when you restart "date usage"
DusmSvc  
 Restart-Service -Name DusmSvc  -Force