How to Use PowerShell to Get an IP Address

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

The internet is full of exciting tools, so much that it can be difficult to find the information you need. In this article, I will offer a quick introduction on how to use PowerShell for getting an IP address quickly and easily.

PowerShell is a command-line tool that allows users to perform many tasks, such as getting an IP address. To do this, type PowerShell get remote ip address. Read more in detail here: powershell get remote ip address.

How to Use PowerShell to Get an IP Address

Working with Windows networking is one of the many things IT administrators can accomplish using PowerShell and the command line. In this article, you’ll learn how to utilize the Collect-NetIPAddress cmdlet with WMI in PowerShell to get IP addresses from network adapters on a Windows PC.

Prerequisites

This lesson assumes you have Windows PowerShell v5.1 installed on a PC running Windows 10 or above. It’s also expected that you’re working with Active Directory. If you haven’t already, familiarize yourself with the Credential property of the New-CimSession cmdlet.

Using the Get-NetIPAddress command

Get-NetAdapter, Get-NetAdapterBinding, and even Get-NetIPAddress are all PowerShell cmdlets for working with network connections. Utilize these cmdlets if you wish to use PowerShell to acquire the IP address in a simple script.

Maintain a straightforward approach. If there’s a simpler method to accomplish anything using PowerShell, take advantage of it!

Run a single line, for example, to discover the IPv4 address on all network devices on a local computer.

Get-NetIPAddress -AddressFamily IPV4 Get-NetIPAddress -AddressFamily IPV4

With Get-NetIPAddress, you can find IPV4 addresses.With Get-NetIPAddress, you can find IPV4 addresses.

Only the IP address is returned in the result above, which refers to a particular network equipment such as the Ethernet adapter.

Obtaining an IP address from a certain adapterObtaining an IP address from a certain adapter

You’ve completed the task! Obtaining an IP address using PowerShell is a simple operation, at least much more so than with other methods.

If you don’t want to use PowerShell, you can retrieve an IP address using the ipconfig command. The Ipconfig Commands You Need to Know has further information about ipconfig.

Why Should You Use CIM/WMI?

When you use the Get-NetIPAddress cmdlet, PowerShell really queries WMI to get the information you need. What is the significance of knowing this? Because you can use WMI to do your own queries. But why utilize WMI when a single command would suffice?

When you’re already querying WMI, one of the most popular reasons to utilize WMI over a single command like Get-NetIPAddress is to save time. WMI includes a wealth of system information that is useful for creating inventory scripts.

When using PowerShell to communicate with WMI, you may construct a CIM session, which works similarly to a PSRemoting session. Using a session is a quick and easy method to connect to a computer once and then reuse the connection.

The Ultimate Guide to PowerShell Remoting

When creating this job as part of a bigger script, use CIM to retrieve an IP address.

Without a Session, Querying CIM

You should utilize a reusable CIM session when writing a PowerShell script that uses CIM to query several types of data. Not only does reusing a single session speed up your script, but it also cuts down on code duplication.

For example, you could want to know what operating system is installed on a remote Windows PC. Inside the Win32 OperatingSystem class, CIM stores the OS name in a property named Caption. You’d use the Acquire-CimInstance cmdlet to get this information, passing it the computer name (REMOTECOMPUTER), the CIM class (Win32 OperatingSystem), and just accessing the Caption field on the object.

(Get-CimInstance -ComputerName REMOTECOMPUTER -ClassName Win32 OperatingSystem -ComputerName REMOTECOMPUTER -ComputerName REMOTECOMPUTER -ComputerName REMOTECOMPUTER -Com Caption

Get-CimInstance requires PowerShell to locate the remote machine, send the logged-in credentials to it, and then query CIM for the information you want. When you perform Get-CimInstance without a session, there’s a lot of overhead.

What if you’re writing a server inventory script and need to know about storage, RAM, networking, and other things as well? You’d have a lot of Get-CimInstance instructions like the ones below in that instance.

-ComputerName Get-CimInstance REMOTECOMPUTER -Win32 LogicalDisk -Win32 LogicalDisk -Win32 LogicalDisk -Win32 LogicalD -ComputerName Get-CimInstance REMOTECOMPUTER -Win32 OperatingSystemWin32 OperatingSystemWin32 OperatingSystemWin32 OperatingSystemWin32 Oper -ComputerName Get-CimInstance REMOTECOMPUTER -ClassName Win32 PhysicalMemory REMOTECOMPUTER -ClassName Win32 PhysicalMemory -ComputerName Get-CimInstance -ClassName Win32 NetworkAdapter REMOTECOMPUTER

In the preceding code sample, your script connects to REMOTECOMPUTER four times, but what if the computer name changes? That machine name will have to be found/replaced, which is never a good indication when coding.

Putting Together a CIM Session

Instead of using Get-CimInstance with a session, you should use the New-CimSession cmdlet to establish a single CIM session that you can reuse again and over.

Instead of repeatedly authenticating to a distant machine, use New-CimSession to establish a single CIM session and send that session to Get-CimInstance, as shown below.

$remoteComputer = ‘REMOTECOMPUTER’; $remoteComputer = ‘REMOTECOMPUTER’; $cimSession = New-CimSession -ComputerName; $cimSession = New-CimSession -ComputerName; $cimSe $remoteComputer -CimSession Get-CimInstance -ClassName Win32 LogicalDisk $cimSession -CimSession Get-CimInstance -ClassName Win32 OperatingSystem $cimSession -CimSession Get-CimInstance -ClassName Win32 PhysicalMemory $cimSession -CimSession Get-CimInstance -ClassName Win32 NetworkAdapter $cimSession

When you’re through with the session, disconnect from it and delete it from your memory.

Remove-CimSession | $cimSession

Using CIM sessions like the ones described above is an excellent way to get started with CIM/WMI.

How to Choose the Right Network Adapter

Because a Windows PC might have many network adapters, you must first choose which network adapter you want to use to get the IP address. Use the Win32 NetworkAdapterConfiguration class when using WMI. This class contains a lot of the data returned by Get-NetIPAddress.

Create a PowerShell script containing the name of the remote machine, a new CIM session, and your CIM session removal line below it.

$remoteComputer = ‘REMOTECOMPUTER’ $cimSession = New-CimSession -ComputerName $remoteComputer Remove-CimSession | $cimSession

Insert the WMI query once you’ve completed the CIM session code.

$remoteComputer = ‘REMOTECOMPUTER’ $cimSession = New-CimSession -ComputerName $remoteComputer Get-CimInstance -CimSession $cimSession -ClassName Win32_NetworkAdapterConfiguration Remove-CimSession | $cimSession

Using a CIM session to query WMI for network adapter informationUsing a CIM session to query WMI for network adapter information

As you can see, the default output does not include the IP address. You’ll need to delve a little deeper. There isn’t even a single instance returned. What’s the best way to find the one with the IP address you’re searching for?

To do so, use Select-Object to see all of the properties.

-CimSession Get-CimInstance Select-Object -Property * $cimSession -ClassName Win32 NetworkAdapterConfiguration

As you scroll through the results, you’ll discover that some network adapters have no IP address, some have one, and still others have numerous IP addresses! You must keep it to a minimum. You’ll need to come up with a common criteria to filter on that will apply to all servers.

The Win32 NetworkAdapterConfiguration class produces a lot of output!The Win32 NetworkAdapterConfiguration class produces a lot of output!

The IPEnabled attribute should be shown on each adapter. The TCP/IP protocol is tied to this NIC when this property is set to True, which is a requirement for having an IP address. You’ll find the adapter you’re seeking for if you limit down the NICs that have the IPEnabled attribute set to True.

It’s advisable to use the Filter argument on Get-CimInstance when filtering WMI instances. The PowerShell community has a slogan that states “filter left.” If you have the opportunity, always filter output as far to the left as feasible, according to this proverb.

If you don’t have to, don’t use Where-Object. Instead, owing to the absence of complexity associated with processing superfluous items throughout the pipeline, performance will be significantly improved.

Related: How to Filter Everything Using Where-Object

Get-Filter CimInstance’s option makes advantage of Windows Query Language (WQL). The WQL language is a subset of SQL. The Filter parameter requires the same WHERE clause constraint syntax as SQL.

Use the WQL query SELECT * FROM Win32 NetworkAdapterConfiguration WHERE IPEnabled = ‘True’ to locate all CIM instances of the Win32 NetworkAdapterConfiguration class with the IPEnabled attribute set to True.

We need to set IPEnabled = ‘True’ for the Filter since we have specified the class name for the ClassName parameter argument in Get-CimInstance. Only the network adapter with the IP address will be returned.

-CimSession Get-CimInstance $cimSession -Filter Win32 NetworkAdapterConfiguration -ClassName Win32 NetworkAdapterConfiguration “IPEnabled = ‘True’” | Select-Object -Property * “IPEnabled = ‘True’”

When you execute the preceding code, you should see that Get-CimInstance only returns one (or a small number of) instances.

Let’s look at what it looks like now that you have a single CIM instance and know the attribute you’re searching for (IPAddress). In this case, it produces a single CIM instance with three strings for the IPAddress attribute, including IPV6 addresses, as seen below.

(Get-CimInstance -CimSession $cimSession -ClassName Win32 NetworkAdapterConfiguration -Filter “IPEnabled = ‘True’” Get-CimInstance -CimSession $cimSession -ClassName Win32 NetworkAdapterConfiguration -Filter “IPEnabled = ‘True’” Get-CimInstance -Cim 192.168.0.40 fe80::e4e1:c511:e38b:4f05 2607:fcc8:acd9:1f00:e4e1:c511:e38b:4f05 2607:fcc8:acd9:1f00:e4e1:c511:e38b:4f05

You’ll need to remove a few more items from the mix. You must now parse out the IPv4 address since WQL can’t filter deeper than the IPAddress property value.

The IPV4 address is usually the first in the array to be specified. Select that one by referring to the 0 index, as illustrated below. You’ll see that it only gives a single IPV4 IP address string if you do so.

(Get-CimInstance -CimSession $cimSession -ClassName Win32 NetworkAdapterConfiguration -Filter “IPEnabled = ‘True’” Get-CimInstance -CimSession $cimSession -ClassName Win32 NetworkAdapterConfiguration -Filter “IPEnabled = ‘True’” Get-CimInstance -Cim IPAddress[0] 192.168.0.40

The IPV4 address isn’t necessarily the first member in the array. It’s possible that the IPAddress attribute isn’t even an array. IP configurations vary greatly across adapters and setups. Filtering them may need some ingenuity.

Conclusion

If you need to acquire an IP address from a machine using PowerShell, start with the Get-NetIPAddress cmdlet. If you’re working on a longer script or can’t use this command for whatever reason, consider using Get-CimInstance and CIM session.

The “powershell get ip address into variable” is a PowerShell command that can be used to retrieve an IP address. It takes the IP address, and stores it in a variable called $ipAddress.

Related Tags

  • powershell get ip address of local machine
  • powershell get ip address from hostname list
  • get-wmiobject ip address
  • powershell script to get ip address from list of servers
  • powershell get all ip addresses on network

Table of Content