Powershell script - IP of local PC

Posted on 2012-09-02
Last Modified: 2012-09-03
I am attempt to write a powershell script that will extract the IP address of a local PC it is being run on.

So far I have written this:

$strComputerID = "."
$Ipinfo = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled=TRUE and DHCPEnabled=TRUE" -comp $strComputerID
# get-member -inputobject $Ipinfo ----------- I am using this line to identify the type of info available
get-member -name IPAddress $IPinfo

What am I missing. I understand $Ipinfo is an array, but I can't extract individual information from it like IPAddress or DefaultIPGateway and so on.

Question by:alienvoice
    LVL 14

    Accepted Solution

    ipconfig | find "IPv4"

    maybe you could say what are you trying to achieve. looks like you are trying something easy to do vey complicated way
    LVL 15

    Assisted Solution

    by:Ian Meredith
    Not sure if this is what you're after, but this script gets the ip address...
    LVL 16

    Assisted Solution

    by:Dale Harris
    Try this:
    ipconfig | Select-String IPv4
    LVL 6

    Assisted Solution

    Because IP Info is an array, you'll can't access a property for the entire array.

    Try $IpInfo[0] for the first item in the array, then $IPInfo[1] for the next, and so on...

    If you want to access all the items in the array

    Try the following:

    $ipinfo | Select -ExpandProperty IPAddress


    $ipinfo | Select -ExpandProperty DefaultIPGateway
    LVL 3

    Assisted Solution

    In single lines:

    For IP Address -
    (get-wmiobject win32_NetworkAdapterConfiguration -filter "IPEnabled=True and DHCPEnabled=True").IPAddress

    For Default Getway:
    (get-wmiobject win32_NetworkAdapterConfiguration -filter "IPEnabled=True and DHCPEnabled=True").DefaultIPGateway

    and so on for
    LVL 15

    Author Comment

    Thanks everyone, suggestions were appreciated. Most of the suggestions worked, I have gone with code as listed below:

    $strComputerID = "."
    $Ipinfo = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled=TRUE and DHCPEnabled=TRUE" -comp $strComputerID

    Reason behind this is, it will allow me to modify the input if needed at a later date and it will run a check to verify the IP is DHCP enabled, (which is important for the situation it will be used in).

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Why Use PowerShell? Recently, I was asked to come up with a quick way to find out what versions all of our SQL servers were currently running. Well, we have several thousand so taking a manual approach simply wasn't an option. We also didn't have t…
    Set OWA language and time zone in Exchange for individuals, all users or per database.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now