title: Powershell convert String to System.Net.IPAddress
body: <p>I'm new to powershell and I'm trying to automate creating a DHCP reservation. </p>
  
  <p>So far I'm able to get the IP address like so: </p>
  
  <pre><code>$IP = ( GEt-VM -ComputerName $HVCOMPUTERNAME -VMName $HVNAME | Get-VMNetworkAdapter).IpAddresses[0]
  </code></pre>
  
  <p>This returns a string like:</p>
  
  <pre><code>192.0.2.1
  </code></pre>
  
  <p>However, the Add-DhcpServer4Resrvation cmdlet does not accept an ip address as a string. It requires the IP address be a 'System.Net.IpAddress'</p>
  
  <pre><code>Add-DhcpServerv4Reservation -ComputerName $DHCPServer -ScopeId $DHCPScope -IPAddress $IP -Client
  Id $MacAddress -Name $HVNAME
  
  Add-DhcpServerv4Reservation : Cannot process argument transformation on parameter 'IPAddress'. Cannot convert value "
  10.254.130.104
  " to type "System.Net.IPAddress". Error: "An invalid IP address was specified."
  At line:1 char:86
  + ... ope -IPAddress $IP -ClientId $MacAddress -Name $HVNAME
  +                    ~~~
      + CategoryInfo          : InvalidData: (:) [Add-DhcpServerv4Reservation], ParameterBindingArgumentTransformationEx
     ception
      + FullyQualifiedErrorId : ParameterArgumentTransformationError,Add-DhcpServerv4Reservation
  </code></pre>
  
  <p>
  How do you convert a string to a System,.Net.IPAddress? </p>
  
  <p>According <a href="http://www.darkoperator.com/blog/2014/6/18/powershell-tip-validating-ip-address-as-a-parameter" rel="nofollow noreferrer">to this link</a>, it should be easy like</p>
  
  <pre><code>&gt; [ipaddress]"192.0.2.1"
  </code></pre>
  
  <p>However that doesn't work. </p>
  
  <pre><code>PS C:\Windows\system32&gt; $FOO = [IPAddress]$IP
  Cannot convert value "
  10.254.130.104
  " to type "System.Net.IPAddress". Error: "An invalid IP address was specified."
  At line:1 char:1
  + $FOO = [IPAddress]$IP
  + ~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
      + FullyQualifiedErrorId : InvalidCastParseTargetInvocation
                     tworkAdapter) Format-List -Property *.254.13༁爼ሂÌGEt-VM -ComputerName $HVCOMPUTERNAME -VMName $HVNAME | Get-VMNetworkAdapter) | Format-List -Property *    {༁牎ᐂÊGEt-VM -ComputerName $HVCOMPUTERNAME -VMName $HVNAME | Get-VMNetworkAdapter | Format-List -Property *
  ఁ牘ࠂÆ$IP = ( GEt-VM -ComputerName $HVCOMPUTERNAME -VMName $HVNAME | Gt-VMNex뿰bpte
  </code></pre>
  
  <p>Related question<br>
  <a href="https://stackoverflow.com/questions/29976454/powershell-get-ip4v-address-of-vm">Powershell, get ip4v address of VM</a>                                                                                                    </p>