PowerShell Script to Find IP Address Easily
Get-NetIPAddress | Where-Object {$_.AddressFamily -eq 'IPv4'} | Select-Object -ExpandProperty IPAddress to find your IPv4 address quickly.Examples
How to Think About It
Algorithm
Code
Get-NetIPAddress | Where-Object {$_.AddressFamily -eq 'IPv4'} | Select-Object -ExpandProperty IPAddressDry Run
Let's trace finding the IP address 192.168.1.10 through the code
Get all IP addresses
Returns a list including IPv4 and IPv6 addresses like 192.168.1.10, fe80::1
Filter IPv4 addresses
Keeps only 192.168.1.10, removes IPv6 addresses
Select IP address property
Outputs just the string '192.168.1.10'
| Step | Action | Result |
|---|---|---|
| 1 | Get all IPs | 192.168.1.10, fe80::1 |
| 2 | Filter IPv4 | 192.168.1.10 |
| 3 | Select IP | 192.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
ipconfig | Select-String 'IPv4' | ForEach-Object { ($_ -split ':')[1].Trim() }
Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress -ne $null } | ForEach-Object { $_.IPAddress | Where-Object { $_ -match '\.' } }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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Get-NetIPAddress | O(n) | O(n) | Modern Windows, clean output |
| ipconfig parsing | O(n) | O(n) | Compatibility with older Windows |
| WMI object | O(n) | O(n) | Older systems, detailed info |
Get-NetIPAddress for a clean and modern way to get IP addresses in PowerShell.