Skip to content

Instantly share code, notes, and snippets.

@tg12
Created March 7, 2024 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tg12/d3715c8c2a84b003d2b6e112eb16638f to your computer and use it in GitHub Desktop.
Save tg12/d3715c8c2a84b003d2b6e112eb16638f to your computer and use it in GitHub Desktop.
A PowerShell script that identifies the interface with the default Internet route, sets it to the highest priority, and verifies connectivity by pinging Google.
# Identify the interface with the default route to the Internet (0.0.0.0 route)
$defaultRoute = Get-NetRoute -DestinationPrefix "0.0.0.0/0" | Sort-Object -Property RouteMetric | Select-Object -First 1
$interfaceIndex = $defaultRoute.InterfaceIndex
# Retrieve all network interfaces
$interfaces = Get-NetIPInterface -AddressFamily IPv4
# Set the priority for the interface with the default route to the highest (lowest metric)
foreach ($interface in $interfaces) {
if ($interface.InterfaceIndex -eq $interfaceIndex) {
Set-NetIPInterface -InterfaceIndex $interface.InterfaceIndex -InterfaceMetric 1
} else {
# Optional: Increase the metric for other interfaces to deprioritize them
Set-NetIPInterface -InterfaceIndex $interface.InterfaceIndex -InterfaceMetric ($interface.InterfaceMetric + 1)
}
}
# Test connectivity to Google's public DNS to confirm the interface has a route to the Internet
$testResult = Test-Connection -ComputerName "google.com" -Count 1 -ErrorAction SilentlyContinue
if ($testResult) {
Write-Output "Successfully pinged Google. Internet access is confirmed on interface with index: $interfaceIndex"
} else {
Write-Output "Failed to ping Google. Check the Internet connection on interface with index: $interfaceIndex"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment