Replace last octet of IP addresses?

I am trying to get a list of IP addresses of the computer I am connected onto.

I have achieved this via:

$IPV4Addresses = Get-NetIPAddress -AddressFamily ‘IPv4’ | Select-Object -ExpandProperty ‘IPAddress’

This outputs as a list of 8 IP addresses. I’m wondering how I can replace the last octet on each IP address so they become xxx.xxx.xx.*

as a bonus I would also like to separate the final output by ;

Is this easy to achieve?

 

since the output is a collection, a simple way would be to iterate through each of them and do split and join.

$IPV4Addresses | ForEach-Object -Process {
   $SplitItem = $_.ToString() -split '\.'
   $SplitItem[3] = $NewLastOctet
   $NewIP = $SplitItem -Join '.'
   $NewIP
}

PS: Untested code

Hi,

Thanks for this the output came out like this:

Output

How can I now format that to separate by ; ?

When working with IPs like that, I usually just use a ‘\d+$’ to replace the last octet.

For your desired output:

 

($IPV4Addresses | foreach {$_ -replace '\d+$','*'}) -join ';'

That works great! thank you for this! :slight_smile: