0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Find IP Address Easily

Use the PowerShell command Get-NetIPAddress | Where-Object {$_.AddressFamily -eq 'IPv4'} | Select-Object -ExpandProperty IPAddress to find your IPv4 address quickly.
📋

Examples

InputRun script on a computer with IP 192.168.1.10
Output192.168.1.10
InputRun script on a computer with multiple IPs 10.0.0.5 and 172.16.0.2
Output10.0.0.5 172.16.0.2
InputRun script on a computer with no IPv4 address
Output
🧠

How to Think About It

To find the IP address, we ask the computer for all network addresses, then pick only the IPv4 ones because they are the common format. We filter out other types like IPv6 and show just the IP numbers.
📐

Algorithm

1
Get all network IP addresses from the system.
2
Filter the list to keep only IPv4 addresses.
3
Extract and display the IP address values.
💻

Code

powershell
Get-NetIPAddress | Where-Object {$_.AddressFamily -eq 'IPv4'} | Select-Object -ExpandProperty IPAddress
Output
192.168.1.10
🔍

Dry Run

Let's trace finding the IP address 192.168.1.10 through the code

1

Get all IP addresses

Returns a list including IPv4 and IPv6 addresses like 192.168.1.10, fe80::1

2

Filter IPv4 addresses

Keeps only 192.168.1.10, removes IPv6 addresses

3

Select IP address property

Outputs just the string '192.168.1.10'

StepActionResult
1Get all IPs192.168.1.10, fe80::1
2Filter IPv4192.168.1.10
3Select IP192.168.1.10
💡

Why This Works

Step 1: Get-NetIPAddress command

This command asks Windows for all IP addresses assigned to the computer.

Step 2: Filter by IPv4

We use Where-Object to keep only addresses where AddressFamily equals 'IPv4', ignoring IPv6.

Step 3: Extract IP address

Finally, Select-Object -ExpandProperty IPAddress shows only the IP address strings, making output clean.

🔄

Alternative Approaches

Using ipconfig and parsing output
powershell
ipconfig | Select-String 'IPv4' | ForEach-Object { ($_ -split ':')[1].Trim() }
Works on all Windows versions but requires parsing text output, which is less clean and slower.
Using WMI object
powershell
Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress -ne $null } | ForEach-Object { $_.IPAddress | Where-Object { $_ -match '\.' } }
Works on older systems but slower and more complex than Get-NetIPAddress.

Complexity: O(n) time, O(n) space

Time Complexity

The script processes each network interface once, so time grows linearly with the number of interfaces.

Space Complexity

It stores the list of IP addresses temporarily, so space grows with the number of addresses.

Which Approach is Fastest?

Using Get-NetIPAddress is fastest and cleanest; parsing ipconfig output is slower and error-prone.

ApproachTimeSpaceBest For
Get-NetIPAddressO(n)O(n)Modern Windows, clean output
ipconfig parsingO(n)O(n)Compatibility with older Windows
WMI objectO(n)O(n)Older systems, detailed info
💡
Use Get-NetIPAddress for a clean and modern way to get IP addresses in PowerShell.
⚠️
Beginners often forget to filter for IPv4 and get confusing IPv6 addresses or empty results.