Removing Leading "0" in IP Addresses

by Feb 28, 2013

Leading "0" in IP addresses can cause confusion because many network commands interpret octets with leading "0" as octal numbers:

# no leading "0":
PS> ping 10.10.5.12
Pinging 10.10.5.12 with 32 bytes of data:
(...)
# leading "0":
PS> ping 010.10.005.012
Pinging 8.10.5.10 with 32 bytes of data:
(...)

To remove leading "0" from IP addresses, use this piece of code:

PS> '010.10.005.012' -replace '\b0*',''
10.10.5.12

Twitter This Tip! ReTweet this Tip!