1

I found this code on the internet and it purports to remove all [,],{,} characters from the IPaddress field. However, it does not work (on PowerShell 5.1). I very much like the concept of this and would like to use a working version of this:

get-wmiobject win32_networkadapterconfiguration | where {$_.IPAddress -ne $Null} | select @{label="IP address";Expression={$_.IPAddress -replace '[{}]',''}}, MACAddress
  • How can I get this to work, i.e., to remove [,],{,} from the IPAddress field?
  • Additionally, how could I split the addresses such that IPv4 are in one column and IPv6 addresses are in another?
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
YorSubs
  • 3,194
  • 7
  • 37
  • 60

2 Answers2

2

The reason why your replace is not working is because you are getting an object back and the replace is looking in that content, where there are none {[]} you can unwrap the list by joining the content to get string from that list.

get-wmiobject win32_networkadapterconfiguration | where {$_.IPAddress -ne $Null} | select @{label="IP address";Expression={$_.IPAddress -join ', '}}, MACAddress

This will give you somthing in the lines of

IP address                               MACAddress
----------                               ----------
111.222.333.1, fe11::2222:3333:c4d4:555a  00:11:22:33:44:55
111.222.33.44, fe11::f222:3a33:c444:555a  55:44:33:22:11:00
111.222.333.1, fe11::b222:ac33:d444:5552  00:A5:B2:C3:4F:5E
Nils Kähler
  • 2,645
  • 1
  • 21
  • 26
2

Your list of IP Addresses are actually arrays, this ({<item1>, <item2>, <itemX>}) is just how an array is displayed in PowerShell. in other words you can just do:

Expression={"$($_.IPAddress)"}

To separate the IP4 addresses from the IP6 addresses you can do something like:

get-wmiobject win32_networkadapterconfiguration |
    Where-Object {$_.IPAddress -ne $Null} |
        Select-Object @{label="IP4 address";Expression={"$($_.IPAddress -like '*.*')"}},
                      @{label="IP6 address";Expression={"$($_.IPAddress -like '*:*')"}},
                      MACAddress
iRon
  • 20,463
  • 10
  • 53
  • 79