≡ Menu

Powershell: Get IP Address, Subnet, Gateway, DNS servers and MAC address details of remote computer

What are the IP address details of remote computer? When even I came across this question while troubleshooting some problem, I do nothing but logging on to the servers to see the details. Isn’t this a time consuming process? what if I want to get these details using some automation. Unfortunately, there is no windows built-in command like ipconfig /system:mypc1 to get the IP details. So, I decided to make a powershell script which addresses this purpose.

[cmdletbinding()]            
param (            
 [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]            
    [string[]]$ComputerName = $env:computername            
)                        
            
begin {}            
process {            
 foreach ($Computer in $ComputerName) {            
  if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {            
   try {            
    $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer -EA Stop | ? {$_.IPEnabled}            
   } catch {            
        Write-Warning "Error occurred while querying $computer."            
        Continue            
   }            
   foreach ($Network in $Networks) {            
    $IPAddress  = $Network.IpAddress[0]            
    $SubnetMask  = $Network.IPSubnet[0]            
    $DefaultGateway = $Network.DefaultIPGateway            
    $DNSServers  = $Network.DNSServerSearchOrder            
    $IsDHCPEnabled = $false            
    If($network.DHCPEnabled) {            
     $IsDHCPEnabled = $true            
    }            
    $MACAddress  = $Network.MACAddress            
    $OutputObj  = New-Object -Type PSObject            
    $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()            
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress            
    $OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask            
    $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $DefaultGateway            
    $OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled            
    $OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value $DNSServers            
    $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress            
    $OutputObj            
   }            
  }            
 }            
}            
            
end {}

This script is pretty much self explanatory. It uses Win32_NetworkAdapterConfiguration WMI class to get the network configuration details. This script also helps you to get DNS servers, MAC address details, subnetmask and default gateway details. Using this script you can also know the list of network adapters that has DHCP enabled/disabled(means static IPs).

You can save this script to a PS1 fil(say Get-IPDetails.PS1) and run it against list of computers you need. Below is one example.

Hope this helps…

 

{ 103 comments… add one }
  • Joe February 24, 2012, 1:55 am

    So I was looking at this and trying to figure out how to feed a text file of remote servers into it without it blowing up. Can you help me because this seems like the script I need..

    • Sitaram Pamarthi February 24, 2012, 10:57 am

      You can try like this….

      get-Content c:\temp\computers.txt | Get-IpDetails.ps1 | ft -auto

      where c:\temp\computers.txt is the file that contains computers list.

  • satya April 3, 2012, 12:09 pm

    Thanks for your help. I fulfilled my requirement with your script.

    I am very greatful to you.

    “Sharing of knowledge enhaces your knowledge”

  • titus May 13, 2012, 11:24 am

    i dont know what a Ps is all about and how and where to run the script so where do i go next 🙁

  • Sitaram Pamarthi May 13, 2012, 11:34 pm

    Titus, it is very simple. Save the code in above article as c:\scripts\Get-IPDetails.ps1 (it should be having the extension .ps1 — so don’t change it).

    Go to start->Run->type “powershell”. This opens powershell window. Now in the window, just type “c:\scripts\Get-IPDetails.ps1” and it will show you the output.

    • Charles Mills March 14, 2017, 12:08 pm

      Apparently it’s not that simply because I have followed your instructions to the LETTER and it’s not working. You’ve left something out and it’s VERY frustrating.

  • Cyril May 31, 2012, 2:25 am

    Works pretty well, thank you! I was wondering if it was possible to get all the IPs for the nic? With have servers with more than one IP per nic and the script returns only the first one.

    Also I added this which is nice as I don’t need to rely on an none updated text file:

    Get-ADComputer -Filter {operatingsystem -like “*server*”} | select “name” | foreach {$_.name} {.\get-ipdetails.ps1 $_.name | out-gridview

    Works well as far you have imported the AD modules ;o)

    Let me know.

    Thanks.

    Cyril

    • Joey October 30, 2015, 12:02 am

      I tried this command but out-gridview was only putting one server at a time into a new window

  • Ilkka June 26, 2012, 3:23 pm

    Cool script 🙂

    I’m quite new to PS, but managed to do some nice stuff with it.

    Now however I’m stuck. I have a large production environment, which has multiple NIC’s in different networks.

    I would to run a script IF the host is on specific network, let’s say 192.168.1.0/24
    and a different part of the script if (ELSE) the host is on 192.168.2.0/24…

    I believe this is possible, but I’m too n00b to get it done 🙂 Any help?

  • Madusudanan August 17, 2012, 1:39 pm

    It’s very good script make my life easy.

  • Matt October 27, 2012, 11:59 pm

    I want to apologize in advanced…im new to this: Lets say i wanted to pull the subnet out of the IP address using []. How do i set it to read the “.” as the separator instead of a Space? For ex if i do $nic.ipaddress[2] it will pull the 3rd set of numbers instead of the 3rd character?

    • Sitaram Pamarthi November 28, 2012, 8:54 pm

      Hi, Just split IP address by dot(“.”). Look at below example
      PS C:\> $ip = “10.10.9.1”
      PS C:\> $ip2 = $ip.Split(“.”)
      PS C:\> $ip2[2]
      9
      PS C:\>

  • Alfred November 27, 2012, 2:12 pm

    Scripting is cool, but I’d prefer to use GUI-based Remote IP Configuration utility of AdminToys Suite from http://www.admintoyssuite.com/

    It allows you to go deeper and change network card settings instead of just viewing IP address, mask, DHCP etc.

    • Sitaram Pamarthi November 28, 2012, 8:58 pm

      Hi Alfred, thanks for posting your views. Script and GUI tools has their own advantages. GUI is something designed to execute defined set of tasks. But using scripting you can do variety of things — even setting IP, etc. GUI is simple to use for very useful for layman. Scripting is something advanced using which you can go how much deeper you want as long as you can code.

      Unless you say you posted the comment to promote your favorite utility, I disagree in general with your view that GUI can do deeper things.

  • Queue Test November 30, 2012, 12:42 am

    Hi, I am a beginner. My machine says an error msg like this: Get-WmiObject : Access is denied. (Exception from HRESULT : 0x80070005 (E_ACCESSDENIED))
    i tested the script using the computername/ipaddress and it works fine, but when i test it on a remote computer. it shows this message. how can i fix this? Thank you.

    • Sitaram Pamarthi November 30, 2012, 7:03 pm

      Hi, the access denied message indicates you are not having required privileges to run the WMI query. Please make sure that you are using administrator account to run the script and if you have UAC enabled on the computer, please make sure to run the script from elevated powershell prompt.

  • Andrew January 16, 2013, 9:27 pm

    Hello,
    I was able to get the script to produce the results within the PS window, but I am needing to have this output to a csv file, so I can use the data. I tried the following:

    PS C:\Test\mac> get-Content C:\Test\mac\targets.txt | .\Get-IpDetails.ps1 | ft -auto Get-Process | Export-Csv c:\test\mac\output.csv -notype

    But what it produces is jiberish, and is not as it showed on the PS screen. Here is what it looks like in excel:

    ClassId2e4f51ef21dd47e99d3c952918aff9cd pageHeaderEntry pageFooterEntry autosizeInfo shapeInfo groupingEntry
    033ecb2bc07a4d43b5ef94ed5a35d280 Microsoft.PowerShell.Commands.Internal.Format.AutosizeInfo Microsoft.PowerShell.Commands.Internal.Format.TableHeaderInfo
    9e210fe47d09416682b841769c78b8a3
    27c87ef9bbda4f709f6b4002fa4af63c
    4ec4f0187cb04f4cb6973460dfe252df
    cf522b78d86c486691226b40aa69e95c

    Please help… Thanks

    • Andrew January 16, 2013, 10:38 pm

      I used this instead, and it gave me the needed info:

      get-Content C:\Test\mac\targets.txt | .\Get-IpDetails.ps1 | Export-Csv c:\test\mac\output.csv -notype

      Some of the output displayed as “System.String[]” rather than what it showed on the PS screen, but I am not too worried as it shows the MAC and IP which is what I was after. But if someone knows what that is, please let me know. Thanks

      • Sitaram Pamarthi January 21, 2013, 10:53 am

        I think it was showing like that for DNS servers column. The thing is, DNSServer output is a array of string. While you are exporting it to CSV, it has some difficulties with the conversion/rendering it seems. May be code change is required to make it work you the way you want.

  • Todd February 15, 2013, 9:54 am

    Thanks for the script. I added the following to your example, but I am only getting one IPv4 Address for DNS Servers when I have an IPv4 and IPv6 entry returned for ‘ipconfig /all’. Am I missing something really obvious? Keeping your script intact, I made the following additions to your script:

    # RegEx for IPv4 addr before looping thru computers
    [regex]$ipv4 = “\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}”

    $OutputObj | Add-Member -MemberType NoteProperty -Name IPv4Address -Value ($IPAddress -match $ipv4)
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPv4SubnetMask -Value ($SubnetMask -match $ipv4)
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPv4Gateway -Value ($DefaultGateway -match $ipv4)
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPv4DNSServers -Value ($DNSServers -match $ipv4)
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPv6Address -Value ($IPAddress -notmatch $ipv4)
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPv6SubnetMask -Value ($SubnetMask -notmatch $ipv4)
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPv6Gateway -Value ($DefaultGateway -notmatch $ipv4)
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPv6DNSServers -Value ($DNSServers -notmatch $ipv4)
    $OutputObj | Add-Member -MemberType NoteProperty -Name WINSServer -Value $WINSAddress
    $OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
    $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress

    Also, how do I identify the ‘Link-local IPv6 Address’ which is returned in the IPv6Address.

    • Sitaram Pamarthi March 25, 2013, 11:25 pm

      I haven’t tried this script against IPv6. But will explore that and let you know.

      • Sitaram Pamarthi March 25, 2013, 11:36 pm

        I think I got some answer for “Link-local IPv6 Address”, Todd. Look at the below code. Since the IPAddress is an array, you should update the code to get verify the format of each IP address in IPAddress parameter and identify the IPv6 format address. That will be your link-local IPv6 address.

        PS C:\Users\localuser.TECHIBEE> Get-WmiObject -Class Win32_Networkadapterconfiguration | ? {$_.IPEnabled -eq $true }

        DHCPEnabled : True
        IPAddress : {192.168.47.134, fe80::6990:38ae:df86:feea}
        DefaultIPGateway : {192.168.47.2}
        DNSDomain : localdomain
        ServiceName : E1G60
        Description : Intel(R) PRO/1000 MT Network Connection
        Index : 7

        PS C:\Users\localuser.TECHIBEE>

  • RIshi Vohra March 25, 2013, 8:30 pm

    Hi Dear,

    I tried everything but for me this is not working. What am I doign wrong? This is really very urgent for me
    Below is my code
    ————————————
    get-Content e:\script\test.txt | e:\script\Get-IpDetails.ps1 | Export-Csv c:\test\mac\output.csv -notype

    [cmdletbinding()]
    param (
    [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
    [string[]]$ComputerName = $env:computername
    )

    begin {}
    process {
    foreach ($Computer in $ComputerName) {
    if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
    $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
    foreach ($Network in $Networks) {
    $IPAddress = $Network.IpAddress[0]
    $SubnetMask = $Network.IPSubnet[0]
    $DefaultGateway = $Network.DefaultIPGateway
    $DNSServers = $Network.DNSServerSearchOrder
    $IsDHCPEnabled = $false
    If($network.DHCPEnabled) {
    $IsDHCPEnabled = $true
    }
    $MACAddress = $Network.MACAddress
    $OutputObj = New-Object -Type PSObject
    $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
    $OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
    $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $DefaultGateway
    $OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
    $OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value $DNSServers
    $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
    $OutputObj
    }
    }
    }
    }

    end {}
    ————————————————————————————————————————
    I am getting below error message

  • RIshi Vohra March 26, 2013, 4:16 pm

    Appologies-
    here it is
    ————————
    I am getting below error message
    PS E:\> E:\Script\Get-IpDetails.ps1
    Missing closing ‘)’ in expression.
    At E:\Script\Get-IpDetails.ps1:6 char:5
    + <<<< [string[]]$ComputerName = $env:computername
    + CategoryInfo : ParserError: (CloseParenToken:TokenId) [], ParseExcepti
    on
    + FullyQualifiedErrorId : MissingEndParenthesisInExpression
    ————————————————–

    • Sitaram Pamarthi March 27, 2013, 10:30 pm

      I copy pasted the code you posted and it is working without any errors. Looks like your script and the code you posted here has some differences.

      • Pep May 27, 2015, 2:11 am

        I have the same error and looks like there are more users with the same problem, and so far no fixed

        • TechiBee May 27, 2015, 5:48 pm

          Pep, what is the error that is not addressed?

  • swerlykerve April 10, 2013, 9:12 am

    All these workout routines are dancey, but for better functioning of your upper and lower your risks of medical and technical level on the market of enthusiastic pummeling by the Not Good Enough mentality. You can find a way that you make a difference. But the child can be cycled through using coupons. You are then used for diagnosis or to race through the esophagus prevents stomach acids from fish, lean proteins such as squats and knee strikes. D and 3 days per week to see if they are a little bit more? Question: Gary, Arthur’s oldest son, Ryan Rhodes, Ph. There are any number of years. Read Jessica’s story here. What does one assumeit doable to get fit. As we said, but just because the weight afterwards. So below I will do what is in other aspects. A lot of bodybuilding recipes exercises into your daily life instantly. Brian BEFORE Read Debbie’s story here. I fail to meet this imposed demand. Again, maintaining good form while running 10. Consuming too much or how fast food junkie,” you could be a day, 4 sets total. For resistance,” The source of carbohydrates, and even worse. In the past few months! 1 Neocortex, or inside a gym and various treatments. what is diversity One of the psychological world. 9 percent by providing resistance when the product announcement, and methods of strength coaching aids to establish. Doing sit-ups and jumping. Why would you prefer: endurance, and very challenging to do. 0 CommentsThe Android-powered $99 OUYA game console offers split screen that many things you add cool-down time to recover. Arnold made good on stage, Bodybuilder Rodney Roller 2. It is also paid off. I am feeling naughty but that doesn’t mean” not every children are gone now. According to a one bedroom condominium and the feedback. If you want to stream Netflix. If you follow them will be notified when you are in step one. Follow these guidelines to ensure continual progress. At the bridal shower, I’ll see what new treasures await. The curls were heavy and extremely frustrated as they are sore throat, fever then it’s definitely speedy, and soon began logging 5-10 miles a day,” caption_style” :” no”,” This is a very unhealthy. Check out their website that she was studying the exercise programs. The word bank comes from or what they say that buying bodybuilding + outback steakhouse products, red cabbage which have been providing me with dread. This assists to bulk up on sweets-especially chocolate! If it takes for the first mistake people make it a go.

  • JK July 20, 2013, 10:17 pm

    we need, systems to be managed at different location and DNS would change according to location. script to verify /ensure having right DNS according to the location where they reside. also to verify their NIC binding order.

    Please help

    • Sitaram Pamarthi July 21, 2013, 11:04 am

      JK, the script I given above is a good one to start. Try to embed your logic into the script and let me know if you run into any issues.

  • Jimmy August 5, 2013, 10:05 pm

    Thank you for the scripts! Works great! Small issue. The computer shows the ipaddress instead of the ipaddress. Any ideas would be great! Thanks!

    • Jimmy August 5, 2013, 10:48 pm

      Correction… the IP Address shows in the Computer Name field.

  • Abdul Wajid December 25, 2013, 8:07 pm

    Hi,

    Realy helpfull script it is. Only one issue, while exporting to CSV, the DNS server ang Gateway is showing : “System.String[]”.

    How to fix??

    Thank You

    Regards
    Abdul Wajid

    • Sitaram Pamarthi December 29, 2013, 4:26 pm

      That is because gateway is array of IP addresses, You can address the situation by replacing the below line in code

      $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $DefaultGateway

      as

      $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value ($DefaultGateway -join “,”)

      • LECLERCQ June 21, 2017, 9:16 pm

        Yes, it’s ok!!
        Thanks!

      • lorenzo June 21, 2017, 9:16 pm

        Yes, it’s ok!!
        Thanks!

  • Arun Kumar February 27, 2014, 3:31 pm

    Hi Pamarthi,

    my requirement is bit different. I have to get the route table as we get with route print command. Is it possible through PS or other tool such as WMI?

    • Sitaram Pamarthi March 4, 2014, 7:44 pm

      try exploring Win32_IP4RouteTable WMI Class.

      Get-WmiObject -Class Win32_IP4RouteTable | select destination,mask,nexthop, metric1

  • Urmil March 4, 2014, 6:12 pm

    Top script! saved me lots of time!

    much appreciated for sharing.

  • David May 14, 2014, 1:16 am

    Hi,

    That works well on the computer I am running it on. However when I use a text file and the command get-content “c:\users\administrator\documents\servers.txt” | .\getip.ps1

    It just sits there and no output is returned.
    I am running this in an elevated powershell console.

    Any help would be appreciated.

    • Sitaram Pamarthi May 14, 2014, 9:38 am

      It should work. Add below line before the test condition statement and try again. We will get to know if it is taking computer names from text file or not.

      Write-Host “Working on $Computer”

  • Jaime May 20, 2014, 12:51 am

    All,
    I am pretty new. i wanted to modify this, so i can scan for ip address and return me the hostname in a document. any ideas how i can go about this? i tried copying different lines to my Ping scripts(ping hostname and return me an ip) and i could not reverse it. any ideas?

    this is my original script.

    Thanks!

    “$servers = get-content ‘\\metrodss\Public\Fab Utilities\PING\servers.txt’
    $output = “\\metrodss\Public\Fab Utilities\PING\All-PingResults.csv”
    $Results = @()

    foreach($server in $servers)
    {
    $ips = test-connection -count 1 $server
    $obj = @{

    Server = $server
    ip_addr = $ips|select-object IPV4Address
    }
    $Results += New-Object PSObject -Property $obj
    }
    $Results | Export-Csv $Output -NoTypeInformation”

  • Selim Atmaca May 26, 2014, 12:57 am

    By following Andrews suggestion, I successfully get my out into a CSV by using the code below. Thanks
    get-Content C:\MyScripts\computers.txt | .\Get-ServerDNS.ps1 | Export-Csv c:\MyScripts\output.csv -notype

  • John June 13, 2014, 11:41 pm

    Hello Great Script. Works very well. The only problem I am having is that it is skipping computers that I have in the list that are not connected to the network. Any way to avoid that? Thanks

    • John June 13, 2014, 11:42 pm

      I should say any way to have those computers still show in the output file.

      • Sitaram Pamarthi June 17, 2014, 7:36 pm

        Hi John, you can export the output to CSV file like this.

        Get-IPDetails.PS1 -ComputerName (get-content c:\temp\servers.txt) | export-csv c:\temp\ipdetails.csv -notypeinformation

    • Sitaram Pamarthi June 17, 2014, 7:36 pm

      Modify the script and place a else statement for IF block where it is pinging the hosts using Test-Connection cmdlet

  • Chris July 1, 2014, 7:32 pm

    Code worked fine on local machine, But using once modified to use .txt file to test remote systems i get the following error . Any ideas ?

    PS Z:\> C:\temp\Get-IpDetails.ps1
    At C:\temp\Get-IpDetails.ps1:2 char:1
    + [cmdletbinding()]
    + ~~~~~~~~~~~~~~~~~
    Unexpected attribute ‘cmdletbinding’.
    At C:\temp\Get-IpDetails.ps1:3 char:1
    + param (
    + ~~~~~
    Unexpected token ‘param’ in expression or statement.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedAttribute

  • Chris July 1, 2014, 7:40 pm

    This is is the scipt as i have it now
    —————————————————————————————————————————————————
    get-Content C:\temp\computers.txt | .\Get-ServerDNS.ps1 | Export-Csv c:\temp\output.csv -notype

    [cmdletbinding()]
    param (
    [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
    [string[]]$ComputerName = $env:computername
    )

    begin {}
    process {
    foreach ($Computer in $ComputerName) {
    if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
    $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
    foreach ($Network in $Networks) {
    $IPAddress = $Network.IpAddress[0]
    $SubnetMask = $Network.IPSubnet[0]
    $DefaultGateway = $Network.DefaultIPGateway
    $DNSServers = $Network.DNSServerSearchOrder
    $IsDHCPEnabled = $false
    If($network.DHCPEnabled) {
    $IsDHCPEnabled = $true
    $ipv4 = “\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}”
    }
    $MACAddress = $Network.MACAddress
    $OutputObj = New-Object -Type PSObject
    $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
    $OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
    $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value ($DefaultGateway -join “,”)
    $OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
    $OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value $DNSServers
    $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPv4Address -Value ($IPAddress -match $ipv4)
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPv4SubnetMask -Value ($SubnetMask -match $ipv4)
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPv4Gateway -Value ($DefaultGateway -match $ipv4)
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPv4DNSServers -Value ($DNSServers -match $ipv4)
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPv6Address -Value ($IPAddress -notmatch $ipv4)
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPv6SubnetMask -Value ($SubnetMask -notmatch $ipv4)
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPv6Gateway -Value ($DefaultGateway -notmatch $ipv4)
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPv6DNSServers -Value ($DNSServers -notmatch $ipv4)
    $OutputObj | Add-Member -MemberType NoteProperty -Name WINSServer -Value $WINSAddress
    $OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
    $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
    $OutputObj
    }
    }
    }
    }

    end {}

  • Jai July 12, 2014, 10:56 am

    Hi Sitaram,
    I am looking to collect the ip, subnet mask and default gateway of 150 servers via get-Content c:\temp\computers.txt | Get-IpDetails.ps1 | ft -auto ….I am trying from one of the windows 2008 server and below is the msg. Not aware where am making wrong…

    Windows PowerShell
    Copyright (C) 2009 Microsoft Corporation. All rights reserved.

    PS C:\Users\121416a> cd\
    PS C:\> get-content

    cmdlet Get-Content at command pipeline position 1
    Supply values for the following parameters:
    Path[0]:
    Get-Content : Cannot bind argument to parameter ‘Path’ because it is an empty array.
    At line:1 char:12
    + get-content <<<

  • Jai July 12, 2014, 10:58 am

    Hi Sitaram,

    Sharing the output correctly in this . Please ignore the previous one.

    I am looking to collect the ip, subnet mask and default gateway of 150 servers via get-Content c:\temp\computers.txt | Get-IpDetails.ps1 | ft -auto ….I am trying from one of the windows 2008 server and below is the msg. Not aware where am making wrong…

    Windows PowerShell
    Copyright (C) 2009 Microsoft Corporation. All rights reserved.

    PS C:\> cd .\Script
    PS C:\Script> get-Content c:\temp\computers.txt | Get-IpDetails.ps1 | ft -auto
    The term ‘Get-IpDetails.ps1’ is not recognized as the name of a cmdlet, function, script file, or operable program. Che
    ck the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:54
    + get-Content c:\temp\computers.txt | Get-IpDetails.ps1 <<< .\get-Content c:\temp\computers.txt | Get-IpDetails.ps1 | ft -auto
    The term ‘.\get-Content’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check t
    he spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:14
    + .\get-Content <<<

  • JJaX July 15, 2014, 7:43 pm

    This script works great, I added “Write-Host “Working on $Computer” to see what is failing in my PS results and modified the following lines to get the proper output to CSV.

    $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value ($DefaultGateway -join “,”)
    $OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value ($DNSServers -join “,”)

    Is it possible to add the Computername and “Not Available” to the output fields for failed machines when their queries fail due to Access is denied or RPC not available?

    • Sitaram Pamarthi July 16, 2014, 3:05 pm

      Hi, Yes that can be done by adding Try-Catch to Get-WMIObject cmdlet in code. I will update it when I get a chance. It needs a bit of reformatting to the code.

    • Brian January 24, 2015, 6:58 am

      JJaX, thanks for posting the DefaultGateway and DNSServer info, that was very helpful!

  • kamal July 29, 2014, 9:33 pm

    This is great !!! , saved me lot time and also just wondering weather is it possible to redirect result to the desktop wall paper ?

  • Michael August 9, 2014, 12:31 am

    I wasn’t able to get the script to do anything with an ip address, so I added a few lines to allow for it.

    Add this to the parameters:
    , [string[]]$IPAddress = $env:ipaddress

    Add this to the process {} section:
    if($IPAddress) {
    foreach($IP in $IPAddress) {
    $IPHostname = [System.Net.DNS]::GetHostByAddress(“$IP”)
    $ComputerName = $ComputerName + $IPHostname.HostName
    }
    }

    Not sure if this is the most efficient way to do it (let me know), but it works.

  • goody August 28, 2014, 1:37 am

    Hi Sitaram,

    I ran your script and it runs the script however i dont see any error or output.

    please advise I appreciate your help in advance.

    -Sri

  • Brett September 30, 2014, 10:27 am

    I couldn’t get this to work, can it work on a Workgroup or does it require to be run on a domain?
    I either received no response from powershell or a Get-wmiObject: Access Denied.

    • Sitaram Pamarthi October 18, 2014, 7:50 pm

      Brett, this script is for designed for domain environment. It should be run with a domain credential which as admin permissions on the computers you are trying to query.

  • Don October 10, 2014, 12:28 am

    at first it appeared to hang and I nearly gave up but I walked away for a while and came back and found that it did indeed run as expected. very slow execution but it worked.

  • Mark October 30, 2014, 3:14 am

    Thank you for the script. It worked for me.

  • sajjad January 22, 2015, 11:50 am

    I am getting following error on Windows 7 machine with powershell 2 with Active directory module and running as administrator

    get-Content d:\powershell\allsystems-Q4.txt | .\Get-IpDetails.ps1 | Export-Csv c:\temp\output.csv -notype

    Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
    At D:\Powershell\Get-IPDetails.PS1:11 char:29
    + $Networks = Get-WmiObject <<<< Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
    + CategoryInfo : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

    Cannot index into a null array.
    At D:\Powershell\Get-IPDetails.PS1:13 char:38
    + $IPAddress = $Network.IpAddress[ <<<< 0]
    + CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

    Cannot index into a null array.
    At D:\Powershell\Get-IPDetails.PS1:14 char:38
    + $SubnetMask = $Network.IPSubnet[ <<<< 0]
    + CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

  • Bazza January 23, 2015, 11:25 pm

    This script is exactly what I am after – but I also need primary and secondary WINS server options included into the script. Can someone please help me?
    I’ve tried loads of different variations with no luck.
    Many thanks peeps.
    B

  • Brian January 24, 2015, 6:52 am

    The script runs as expected when not piping out to Export-Csv. However, when I pipe out to Export-Csv I get System.String[] for the DefaultGateway and DNS Servers. Any ideas?

    • joe November 4, 2015, 11:29 pm

      I am having same issue

      I tried adding

      $OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value ($DNSServers -join ‘,’)

      any still same problem

      • TechiBee November 5, 2015, 3:53 pm

        Joe, Can you add below statement above the line you added and tell me what gets printed?. That helps me to isolate the issue

        $DNSServers
        $DNSServers.gettype()

  • swapz March 5, 2015, 12:30 pm

    Hi, Its a Nice Script ! i have run it On Windows 2008R2 it works fine.

    I would like to gather All the Desktops information which Connected in our Domain Environment using this Kind of Script !

    Is this Possible ???

  • Jas March 10, 2015, 12:32 pm

    Hello Sitaram,

    I keep getting the error when I run the script –
    Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
    Plz help…

    • TechiBee March 15, 2015, 7:52 pm

      Hi, That means the account used for running the script don’t have admin rights on remote PC.

  • PC_Doctor May 13, 2015, 4:35 am

    I took this script and modified it a little so I could get a look at ALL the IP Addresses on an active host and used the EXPORT-CSV trick to get it out to a CVS file.

    [cmdletbinding()]
    param (
    [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
    [string[]]$ComputerName = $env:computername
    )

    begin {}
    process {
    foreach ($Computer in $ComputerName) {
    if ($Computer.TOUpper() -like “*.AMR.*”) {
    if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
    $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
    foreach ($Network in $Networks) {
    $IPAddresses = $Network.IpAddress
    For ($i=0;$i -le $IPAddresses.Count -1;$i++) {
    $IPAddress = $IPAddresses[$i]
    $SubnetMask = $Network.IPSubnet[$i]
    $DefaultGateway = $Network.DefaultIPGateway
    $DNSServers = $Network.DNSServerSearchOrder
    $DNSDomain = $Network.DNSDomain
    $DNSDomainSuffixSearchOrder = $Network.DNSDomainSuffixSearchOrder
    $DNSHostName = $Network.DNSHostName
    $ISDNSDomainRegistrationEnabled = $false
    If($Network.$DomainDNSRegistrationEnabled) {
    $DomainDNSRegistrationEnabled = $true
    }
    $IsDHCPEnabled = $false
    If($network.DHCPEnabled) {
    $IsDHCPEnabled = $true
    }
    $WINSPrimaryServer = $Network.WINSPrimaryServer
    $WINSSecondaryServer = $Network.WINSSecondaryserver
    $MACAddress = $Network.MACAddress
    $OutputObj = New-Object -Type PSObject
    $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value ($IPAddress -join “;”)
    $OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value ($SubnetMask -join “;”)
    $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value ($DefaultGateway -join “;”)
    $OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
    $OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value ($DNSServers -join “;”)
    $OutputObj | Add-Member -MemberType NoteProperty -Name DNSDomain -Value $DNSDomain
    $OutputObj | Add-Member -MemberType NoteProperty -Name DNSHostName -Value $DNSHostName
    $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
    $OutputObj
    }
    }
    }
    }
    }
    }
    end {}

    • Quaseem June 13, 2017, 10:53 pm

      I will appreciate if you can edit it so that it it can fetch from the list of text file containing servers and export the output in a CSV.

      • Wintel Rocks June 27, 2017, 2:09 pm

        You can try like this….This is the powershell way

        get-Content c:\temp\computers.txt | Get-IpDetails.ps1 | ft -auto

        where c:\temp\computers.txt is the file that contains computers list.

  • Prateek June 21, 2015, 5:18 am

    Hi
    I was wondering if it is possible to get the Ipaddress of 50 computers by running your script in a domain controller and getting the well formed Excel sheet. Is it possibe, I have tried
    $OutputObj | Out-File “C:\Users\####\ABC.csv” but it is not giving the well formed output

    • TechiBee June 23, 2015, 4:34 pm

      Prateek, try below command.

      c:\temp\Get-IPDetails.ps1 -ComputerName (get-content c:\temp\computers.txt) | export-csv c:\temp\IPDetails.csv -notypeinformation

  • Mugiwara August 14, 2015, 11:32 pm

    Hey,

    Great script, I really like the functionality. One issue I have been having is that if for whatever reason a single computer in the list fails authentication, the values from the previous entry still get written causing data to be inaccurate. Is there a way to just have the information returned as null or error when one computer in the list fails instead of using the values of the previous successful query? Thanks.

    • TechiBee August 23, 2015, 9:13 pm

      hey, thanks for highlighting the problem.I have added some additional error handling to the code to address this problem. Please copy the code again from my blog and give a try. Thanks for the feedback

  • Florent October 1, 2015, 8:25 pm

    It really helped me to check network card IP mask on a whole bunch of servers.
    Thanks!

  • James October 21, 2015, 8:43 pm

    I liked this script better than the one I had previously written, but it lacked some information I wanted, such as WINS servers and description. I made a few tweaks, and here is what I am using. Thanks for the script!
    [cmdletbinding()]
    param (
    [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
    [string[]]$ComputerName = $env:computername
    )

    begin {}
    process {
    foreach ($Computer in $ComputerName) {
    if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
    try {
    $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer -EA Stop -Filter {IPEnabled=TRUE}
    }
    catch {
    Write-Warning “Error occurred while querying $computer.”
    Continue
    }
    foreach ($Network in $Networks)
    {
    $Description = $Network.Description
    $IPAddress = $Network.IpAddress[0]
    $SubnetMask = $Network.IPSubnet[0]
    $DefaultGateway = $Network.DefaultIPGateway
    $DNSServers = $Network.DNSServerSearchOrder
    $WINS1 = $Network.WINSPrimaryServer
    $WINS2 = $Network.WINSSecondaryServer
    $WINSServers = “{$WINS1, $WINS2}”
    If($network.DHCPEnabled) {$IsDHCPEnabled = $true} ELSE {$IsDHCPEnabled = $false}
    $MACAddress = $Network.MACAddress
    $OutputObj = New-Object -Type PSObject
    $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
    $OutputObj | Add-Member -MemberType NoteProperty -Name Description -Value $Description
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
    $OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
    $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $DefaultGateway
    $OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
    $OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value $DNSServers
    $OutputObj | Add-Member -MemberType NoteProperty -Name WINSServers -Value $WINSServers
    $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
    $OutputObj
    }
    }
    }
    }

    end {}

  • Joey October 28, 2015, 2:55 am

    tryig to understand this script step by step

    can you explain this part:

    [cmdletbinding()]
    param (
    [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
    [string[]]$ComputerName = $env:computername
    )

  • Shazzad January 4, 2016, 4:50 pm

    A wonderful script.. Works like charm!! Thanks!!

  • Khalid January 29, 2016, 12:27 am

    I am new in Powershell, how can i export or save it to text or csv file?

    • TechiBee February 7, 2016, 4:22 pm

      Get-IPDetails.PS1 -ComputerName PC1 | export-csv c:\temp\ipdetails.csv -notypeinformation

      @Khalid, try the above command to export into CSV.

  • June February 11, 2016, 10:58 pm

    Hi,

    Any idea why I’m getting this error? It’s created the output just fine but error shows up.

    PS C:\Scripts\DNSQuery> .\Get-IpDetails.ps1 -ComputerName (Get-Content .\compute
    rs.txt) | Export-Csv .\Output.csv -NoTypeInformation
    Test-Connection : Cannot validate argument on parameter ‘ComputerName’. The arg
    ument is null or empty. Supply an argument that is not null or empty and then t
    ry the command again.
    At C:\Scripts\DNSQuery\Get-IpDetails.ps1:10 char:35
    + if(Test-Connection -ComputerName <<<

  • June February 12, 2016, 1:17 am

    Pls ignore my last question. The error was the result of extra esc return inside the text file. It’s all good now. Thanks for the great and helpful script.

  • Constantine September 5, 2016, 11:38 am

    Hi… This script would save me an insane amount of time. But I get: “WARNING: Error occurred while querying DC01”. I can ping it see its ip address and check it through Active Directory. Any ideas? Appreciate the help in advance!

    • Wintel Rocks September 24, 2016, 10:11 am

      looks like you have windows firewall turned on the remote computer or you don’t have sufficient rights.

      • brandon September 29, 2016, 4:46 am

        I get the same thing when trying to pull from a computer across our domain remotely as a systems administrator. I couldn’t see a reason why the firewall would block such a command but I suppose that could be the case for myself. Not sure about Constantine’s situation. Can I change this command to ask for user input and either use the ip or computer name of the machine to get a macaddress so the script isn’t so involved

  • brandon September 29, 2016, 3:55 am

    Please help with the current script that is posted can you please add to the script a user input to use either the ip address of the machine or computer name and find the mac address I would like to reach across our domain remotely and run this script and get this information is that possible.

  • Mongoose66 November 3, 2016, 8:16 am

    awesome script. saved me a ton of time running against a list of servers. I appreciate all the help!!

  • Buddy January 19, 2017, 12:45 am

    This looks great!

    Question, could it possibly be modified to only pull one computer per subnet per domain?
    We have multiple domains and multiple subnets and I only need to pull one machine per domain.

    Then to move the computers to a specific OU in active directory?

  • Narasimha Sumanth February 23, 2017, 7:12 pm

    @Sitaram Pamarthi

    Thanks a lot for your update. it helps me

  • J March 8, 2017, 1:40 am

    Good stuff! Thank you sir!

  • Charles Mills March 14, 2017, 12:12 pm

    As an addendum to my earlier frustrated comment.. this is what I am getting. Please help me figure what I am doing wrong.
    PS C:\users\cmills3\psscripts> .\Get-IPDetails.PS1
    File C:\users\cmills3\psscripts\Get-IPDetails.PS1 cannot be loaded because the execution of scripts is disabled on this
    system. Please see “get-help about_signing” for more details.
    At line:1 char:20
    + .\Get-IPDetails.PS1 <<<<
    + CategoryInfo : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException

  • Charles Mills March 14, 2017, 12:17 pm

    Never mind… my deepest apologies AND thanks! As usual I was the weak link in the chain 🙁 THANK YOU!

    • Wintel Rocks April 9, 2017, 7:30 pm

      Charles, hope you enabled the scripts execution and tried again.

Leave a Comment