stringpowershellipselect-object

PowerShell regex replace inside a select


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

Solution

  • 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