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?
1
  • Did you mean $_.IPAddress -replace '[][{}]'? Commented Jun 13, 2022 at 11:00

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.