Approach 1
$ip=get-WmiObject Win32_NetworkAdapterConfiguration|Where {$_.Ipaddress.length -gt 1}
$serverIP = $ip.ipaddress[0]
Approach 2
$computername = $env:COMPUTERNAME
#Test-Connection $computername -count 1 | select Address,Ipv4Address
Test-Connection $computername -count 1 | select @{Name="Computer Name";Expression={$_.Address}},@{Name="IPv4 Address";Expression={$_.Ipv4Address}}
Approach 3 – More Details
$colItems = Get-WmiObject Win32_NetworkAdapterConfiguration -Namespace "root\CIMV2" | where{$_.IPEnabled -eq "True"}
clear
Write-Host "# --------------------------------------"
Write-Host "# Local IP address information"
Write-Host "# --------------------------------------"
foreach($objItem in $colItems) {
Write-Host "Adapter:" $objItem.Description
Write-Host " DNS Domain:" $objItem.DNSDomain
Write-Host " Hostname:" $objItem.PSComputerName
Write-Host " IPv4 Address:" $objItem.IPAddress[0]
Write-Host " IPv6 Address:" $objItem.IPAddress[1]
Write-Host " "
}
Originally Posted on November 12, 2015
Last Updated on March 10, 2016
Last Updated on March 10, 2016
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.