PowerShell: IP Address Details

It irritates me that when I execute the command “IPConfig” to get my current DHCP IP Address, I get a large list of all kind of adapters in which I am not interested. I then have to scroll up in the Command Prompt windows to find my IP Address.

Command Prompt

As a project of my own to get to learn PowerShell more, I decided to make a PowerShell Function which would give me the current IP Address. As I went along, I added an extra feature which displayed my current external IP Address.

Below is the PowerShell Function:

Function GetMyIP
{
$NetworkAdapter = gwmi win32_networkadapterconfiguration | where {$_.DHCPEnabled -and $_.DNSDomain -ne $null}
$SourcePage = "href="http://automation.whatismyip.com/n09230945.asp">http://automation.whatismyip.com/n09230945.asp"
$WebClient = new-object System.Net.WebClient
$ExternalIP = $WebClient.downloadString($SourcePage)

Write-host "IP Address IPV4: "$networkadapter.IPAddress[0]
Write-host "IP Address IPV6: "$networkadapter.IPAddress[1]
Write-host "Gateway Address:" $Networkadapter.DefaultIpGateway
write-host "External IP Address:" $ExternalIP
}

3 thoughts on “PowerShell: IP Address Details

  1. This is useful and a good start, but you should take this further and write objects to the pipeline. For example, right now if you wanted to save the output to a text file you couldn’t. Here’s an alternative:

    Function Get-MyIP
    {
    $NetworkAdapter = get-wmiobject win32_networkadapterconfiguration | where {$_.DHCPEnabled -and $_.DNSDomain -ne $null}
    $SourcePage = “http://automation.whatismyip.com/n09230945.asp”
    $WebClient = new-object System.Net.WebClient
    $ExternalIP = $WebClient.downloadString($SourcePage)
    New-Object -TypeName PSObject -Property @{
    IPV4=$networkadapter.IPAddress[0]
    IPV6=$networkadapter.IPAddress[1]
    Gateway=$Networkadapter.DefaultIpGateway[0]
    ExternalIP=$ExternalIP
    Computername=$NetworkAdapter.__SERVER
    }
    }

    You get the same information but this writes a pipelined object. You could extend this to get information from remote computers as well which could come in handy for troubleshooting. Finally, a recommended scripting best practice is to use the same verb-noun naming convention. Notice the slight tweak I made. This makes the function more discoverable. Hope this helps.

  2. Thank you very much Jeffery.
    I’m going to try that it tomorrow, I noticed my script also fails if you are not connected to a domain (Cannot index into a null array).
    You’re suggestion of using the verb-noun naming was something I thought about when I was driving home. I should have seen it before, but I was a bit excited I got it to work, so I posted it a bit to soon (It might seem a simple thing to be excited about to you guru’s, but we have a saying here in holland: “who’m ever doesn’t appreciate the little things, isn’t ready for big things :-) )

  3. Hi Jeffery,

    I updated the script a bit, this one will now find active adapters and return the information for all of them. When a adapter has no gateway (VMware adapters for instance in my situation), it will not query for the external IP.

    Function Get-MyIP2
    {
    $externalIP = “”
    $activeAdapters = Get-WmiObject Win32_NetworkAdapter -Filter ‘NetConnectionStatus=2′
    Foreach ($NWadapter in $activeadapters)
    {
    $Adapter = get-wmiobject win32_networkadapterconfiguration | where {$_.Description -eq $NWadapter.Name}
    if ($Adapter.DefaultIPGateway -ne $null)
    {
    $SourcePage = “http://automation.whatismyip.com/n09230945.asp”
    $WebClient = new-object System.Net.WebClient
    $ExternalIP = $WebClient.downloadString($SourcePage)
    }
    New-Object -TypeName PSObject -Property @{
    IPV4=$Adapter.IPAddress[0]
    IPV6=$Adapter.IPAddress[1]
    Gateway=[string]$Adapter.DefaultIpGateway
    ExternalIP=$ExternalIP
    Computername=$Adapter.__SERVER
    }
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>