Powershell get ipv4 address into a variable

developer747 picture developer747 · Dec 3, 2014 · Viewed 112.8k times · Source

Is there an easy way in powershell 3.0 Windows 7 to get the local computer's ipv4 address into a variable?

Answer

Lucas picture Lucas · Jun 21, 2017

Here is another solution:

$env:HostIP = (
    Get-NetIPConfiguration |
    Where-Object {
        $_.IPv4DefaultGateway -ne $null -and
        $_.NetAdapter.Status -ne "Disconnected"
    }
).IPv4Address.IPAddress