Last updated on July 11, 2023
To get the IP address of a system using PowerShell, you can use the Get-NetIPAddress
cmdlet. Here’s an example:
$ipAddresses = Get-NetIPAddress | Where-Object { $_.AddressFamily -eq 'IPv4' -and $_.PrefixOrigin -eq 'Manual' }
foreach ($ip in $ipAddresses) {
Write-Host "InterfaceAlias: $($ip.InterfaceAlias)"
Write-Host "IP Address: $($ip.IPAddress)"
Write-Host "Subnet Mask: $($ip.SubnetMask)"
Write-Host "Default Gateway: $($ip.DefaultGateway)"
Write-Host "DNS Servers: $($ip.DNSServer)"
Write-Host "-----------------------------"
}
In this script, we assign the IP address string to the $ipAddressString variable. We then use the TryParse method of the System.Net.IPAddress
class to attempt parsing the string as an IP address. The method returns a boolean value indicating whether the parsing was successful.
If the IP address string is valid, the script displays a message confirming its validity. Otherwise, it shows a message indicating that the IP address is not valid.
You can replace the $ipAddressString
variable with the IP address string you want to check. Feel free to integrate this IP address validation logic into your existing scripts or modify it as per your specific requirements.