Replace last octet of IP addresses?

Welcome Forums General PowerShell Q&A Replace last octet of IP addresses?

Viewing 3 reply threads
  • Author
    Posts
    • #283240
      Participant
      Topics: 2
      Replies: 2
      Points: 23
      Rank: Member

      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?

       

    • #283246
      Senior Moderator
      Topics: 9
      Replies: 1449
      Points: 5,426
      Helping Hand
      Rank: Community MVP

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

      PS: Untested code

    • #283252
      Participant
      Topics: 2
      Replies: 2
      Points: 23
      Rank: Member

      Hi,

      Thanks for this the output came out like this:

      Output

      How can I now format that to separate by ; ?

    • #283288
      Participant
      Topics: 4
      Replies: 427
      Points: 756
      Helping Hand
      Rank: Major Contributor

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

      For your desired output:

       

      • #283303
        Participant
        Topics: 2
        Replies: 2
        Points: 23
        Rank: Member

        That works great! thank you for this! 🙂

Viewing 3 reply threads
  • You must be logged in to reply to this topic.