Sorting IP adresses in excel using powwrshell
We all know the problem with IP addresses in excel, I've created a workaround in powershell by adding leading zeros to octets that contains not 3 digits.
To convert an IP address to the format "000.000.000.000" in PowerShell, you can use the -f operator with a format string that includes placeholders for each octet of the IP address. For example:
$ip = "192.168.0.1"
$formattedIp = "{0:000}.{1:000}.{2:000}.{3:000}" -f $ip.Split(".")
# Output: "192.168.000.001"
And to turn it back
$formattedIp = "{0:000}.{1:000}.{2:000}.{3:000}" -f $ip.Split(".")
$formattedIp = $formattedIp -replace "^0*", ""
# Output: "192.168.0.1"
Comments