Amazon holiday deals See deals →ad

Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

Powershell List Devices on Network

You can list devices on your network using PowerShell by querying the ARP cache with Get-NetNeighbor or scanning subnets with Test-Connection. The most common mistake is assuming a single command reveals every connected device; in reality, you must combine local cache data with active subnet probing to get a complete picture.

Quick Answer: The Best Command for Local Cache

If you want an immediate list of devices that your computer has recently communicated with, use the Get-NetNeighbor cmdlet. This command reads the Address Resolution Protocol (ARP) table, which maps IP addresses to physical MAC addresses on your local segment.

To see this data clearly, pipe the output into a formatted table:

Get-NetNeighbor | Format-Table -AutoSize

This will show you the Interface Index, IPv4 Address, Link-Layer Address (MAC), and State. Devices marked as “Reachable” are currently active on the network. Those marked as “Stale” or “Incomplete” may be offline or disconnected.

Why Your ARP Table Is Incomplete

The ARP cache only contains entries for devices your PC has talked to recently. If a printer, smart TV, or IoT sensor has been idle for hours or days, it will not appear in Get-NetNeighbor. To find these silent devices, you need to actively probe the network range.

Your computer knows its own subnet mask and IP address. You can extract these values automatically to build a scan range without manually calculating the network ID. This ensures you target the correct broadcast domain.

How to Scan the Entire Subnet

Active scanning involves sending a small packet (ICMP echo request) to every possible IP address in your local subnet. PowerShell makes this straightforward using Test-Connection.

First, determine your current IPv4 address and subnet prefix length:

$ip = Get-NetIPAddress -AddressFamily IPv4 | Where-Object {$_.InterfaceAlias -like "Ethernet*" -or $_.InterfaceAlias -like "Wi-Fi*"}
$subnetPrefix = $ip.PrefixLength
$currentIP = $ip.IPv4Address

Next, generate a list of all potential IP addresses in that subnet. For a standard home network (/24 subnet), this means checking 254 addresses:

$networkBase = [System.Net.IPAddress]::Parse($currentIP).GetAddressBytes()

Convert to a string format for iteration

$startByte = 1 $endByte = 254 $results = @() foreach ($i in $startByte..$endByte) { $testIP = "{0}.{1}" -f $networkBase[0], $networkBase[1], $networkBase[2], $i try { $ping = Test-Connection -ComputerName $testIP -Count 1 -Quiet -ErrorAction Stop if ($ping) { $results += $testIP } } catch {} }

This script loops through each IP, attempts a connection, and collects the ones that respond. The -Quiet flag reduces output noise, returning only true or false. The -Count 1 parameter sends just one ping packet, making the scan fast.

Resolving Names and Identifying Devices

Knowing an IP address like 192.168.1.50 does not tell you what device it is. It could be a laptop, a phone, or a server. To identify the hardware, you need the MAC address and potentially the hostname.

Combine the results from your subnet scan with Get-NetNeighbor to get MAC addresses:

$identifiedDevices = foreach ($ip in $results) {
    $neighbor = Get-NetNeighbor -AddressFamily IPv4 -IPAddress $ip -ErrorAction SilentlyContinue
    if ($neighbor) {
        [PSCustomObject]@{
            IPAddress = $ip
            MACAddress = $neighbor.LinkLayerAddress
            Status = $neighbor.State
        }
    }
}

For more detailed identification, you can attempt to resolve the NetBIOS name or DNS record associated with each IP. This often requires administrative privileges or specific firewall rules allowing name resolution queries.

Using WMI for Detailed Hardware Info

If you have access credentials for a discovered device, you can query Windows Management Instrumentation (WMI) to get the computer name, manufacturer, and model. This step is optional but helpful for inventorying known machines.

Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $ip -ErrorAction SilentlyContinue | Select-Object CSName, Manufacturer, Model

Note that this command will fail if the remote machine blocks WMI traffic or if you lack permissions. Do not rely on this for every device in the scan.

Handling Firewalls and Security Groups

Not all devices will respond to pings. Many modern operating systems block ICMP Echo Requests by default for security reasons. Windows Firewall, macOS filters, and Linux iptables rules often drop these packets silently.

If a device is online but does not respond to Test-Connection, it will not appear in your scan results. This is normal behavior and not an error in your script. To mitigate this, you can try scanning common ports instead of pinging.

Port Scanning as an Alternative

Some devices listen on specific ports even if they ignore pings. For example, web servers listen on port 80 or 443, and printers often respond on port 9100. You can use TcpClient to test connectivity to specific ports.

$tcpClient = New-Object System.Net.Sockets.TcpClient
try {
    $connected = $tcpClient.ConnectAsync($ip, 9100).Wait(1000)
    if ($connected) { Write-Host "$ip is listening on port 9100" }
} finally {
    $tcpClient.Close()
}

This method is slower than pinging because it establishes a full TCP handshake. Use it selectively for devices you suspect are online but hidden from ICMP scans.

Exporting and Managing Results

Once you have a list of active devices, save the data for future reference. Exporting to CSV allows you to open the list in Excel or other spreadsheet tools for filtering and sorting.

$identifiedDevices | Export-Csv -Path "NetworkDevices.csv" -NoTypeInformation

Include the timestamp when you ran the scan. Network configurations change frequently, so context matters. A device found today might be gone tomorrow.

Common Pitfalls in Network Discovery

Users often encounter issues where scripts run indefinitely or return empty lists. Understanding these causes helps troubleshoot effectively.

  • Virtual Adapters: VirtualBox, VMware, and Docker create virtual network adapters. These can generate phantom IPs that do not correspond to physical devices. Filter your interface selection to exclude virtual adapters if possible.
  • IPv6 vs IPv4: Modern networks often prioritize IPv6. Ensure your scripts handle both address families if your environment uses dual-stack networking. Use -AddressFamily IPv6 in relevant cmdlets.
  • Rate Limiting: Sending too many pings too quickly can trigger intrusion detection systems (IDS) or firewalls to block your source IP. Add small delays between probes if scanning large ranges.

When to Use Third-Party Tools

PowerShell is powerful but manual. For frequent network auditing, consider dedicated network scanner software. These tools automate ARP scanning, SNMP queries, and DNS lookups in a single interface.

However, for quick checks or automation within larger scripts, PowerShell remains the most flexible option. It requires no installation beyond the OS itself and integrates seamlessly with other system administration tasks.

Verifying Your Results

Always cross-reference your PowerShell findings with visual confirmation. Check your router’s admin page for a “Connected Devices” list. Compare the MAC addresses and IP assignments. Discrepancies usually indicate devices blocking discovery methods or network segmentation issues.

If you find devices that should be there but are missing, check their network settings. Ensure they are set to obtain an IP address automatically via DHCP unless static assignment is required. Verify that their firewalls allow inbound discovery traffic.

Powershell List Devices on Network
Scroll to top