Skip to content

Instantly share code, notes, and snippets.

@Jaid
Created October 4, 2024 00:42
Show Gist options
  • Save Jaid/0da15d0c1038531346a2d25d5f25a94d to your computer and use it in GitHub Desktop.
Save Jaid/0da15d0c1038531346a2d25d5f25a94d to your computer and use it in GitHub Desktop.
Get best local IPv4 address in PowerShell script
(Get-NetIPAddress -AddressFamily IPv4 -InterfaceIndex (Get-NetRoute -DestinationPrefix '0.0.0.0/0' | Where-Object { (Get-NetAdapter -InterfaceIndex $_.InterfaceIndex).InterfaceType -eq 6 } | Sort-Object Metric | Select-Object -First 1).InterfaceIndex | Where-Object { $_.IPAddress -notlike '127.*' }).IPAddress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment