Sneh Shah
Sneh Shah

Reputation: 164

How to sort the list if IP address fetched using Win32_NetworkAdapterConfiguration

I am fetching the list of IP addresses of an adapter using Win32NetworkAdapterConfiguration:

$ip= $ip=Get-WmiObject Win32_NetworkAdapterConfiguration | 
select IPAddress

The list of IP addresses comes as:

{192.168.1.10, 192.168.1.9, 192.168.1.8.....}

is there an easy way to sort this list in ascending order? I need the out put like:

{192.168.1.1, 192.168.1.2, 192.168.1.3.....}

Upvotes: 0

Views: 1315

Answers (1)

Olegp
Olegp

Reputation: 182

This bit of code should work. You can sort objects by Sort-Object

Get-WmiObject Win32_NetworkAdapterConfiguration | Sort-Object IPAddress | select IPAddress

Edit:

Since you are getting all those IPs from the same adapter the curly braces {} turn it into an object powershell is struggling to sort. Back to arrays!

$ip = {192.168.1.10, 192.168.1.9, 192.168.1.8, 192.168.1.1}  
$ip =$ip.ToString()
$a= $ip.Split(",")
$a | sort

You will have to filter out mac addresses: create a new array with only ips:

$new=for ($i=0;$i -lt $array.count;$i+=2) {$a[$i]}

So, this should work:

$ip= $ip=Get-WmiObject Win32_NetworkAdapterConfiguration | select IPAddress
$ip =$ip.ToString()
$a= $ip.Split(",")
$new=for ($i=0;$i -lt $array.count;$i+=2) {$a[$i]}
$new | sort

Upvotes: 1

Related Questions