Home > Enterprise >  How do I permanently set a static IP Address in Windows 10 using PowerShell for my automated OS buil
How do I permanently set a static IP Address in Windows 10 using PowerShell for my automated OS buil

Time:12-17

When I started learning how to do this by watching videos, I learned about IP address InterfaceIndexes. I learned that I can't set an IP Address without knowing the IP Address's InterfaceIndex first. That seemed to be different on every computer I look at and seemed to be random. Then, I learned that you can use an IP Address Alias. On a fresh Windows install, it seemed that the IP Address Alias Ethernet0 was going to be a constant that I could rely on. So, with my primitive PowerShell skills, I thought I could reach out and grab the PC's IP Address and check to see if it matched what it was supposed to be. If it wasn't, wipe out whatever Ethernet0 is, and create a new IP Address with the alias of Ethernet0. Of course the first time I did this it worked perfectly and I moved on. I had a static IP address called Ethernet0 with the specified IP address. I just went in and looked and I have 2 available NICs. One is called "Ethernet" and the other is "Ethernet2". Nether of them have a static IP Address. I'm assuming the PC wants to set random alias names upon a restart? I'm a little confused on what happened. Of course when I run my method again, I get an error that says "No InterfaceAlias equal to ethernet0", so my code doesn't work. I'm obviously way off on how I should be approaching this. I want to do a fresh install and run a configuration script to set everything up without any GUI interaction. I don't work in an enterprise domain type of environment, and have to create my own process. Please help further my PowerShell wisdom, as only a Padawan I am.

function SetIp {

    $ipv4 = (Test-Connection -ComputerName $env:COMPUTERNAME -Count 1) .IPV4Address.IpAddressToString

    if($ipv4 -eq "my.ip.add.res") {
        Write-Host "Do Nothing as the IP has already been set"
    }
    else {
        Remove-NetIPAddress -InterfaceAlias ethernet0
        New-NetIPAddress -IPAddress my.ip.add.res -InterfaceAlias ethernet0 -PrefixLength 16
        Write-Host "IP Address has been set"
    }
}

CodePudding user response:

You can see all the available info about the NetIPInterface objects with Get-NetIPInterface | Select * -First 1. Some useful properties are ConnectionState,AddressFamily, or Dhcp. For example, try something like this to find valid ones:

Get-NetIPInterface | Where { 
  $_.ConnectionState -eq 'Connected' -and
  $_.AddressFamily -eq 'IPV4' -and
  $_.InterfaceAlias -like 'Eth*' -and
  # check if in your local subnet
  ($_.DHCP -eq 'Enabled' -or ($_|Get-NetIPAddress).IPv4Address -like '1.2.3.*')
}

You can run into all sorts of things though, so it depends on how controlled your environment is:

  • Wifi and/or Ethernet are connected
  • Second Ethernet, or laptop dock creates new net interfaces
  • VPN software creates a new 'Ethernet' interface

If you need to ask a user for input, you could do it with gridview:

$Selected = Get-NetIPInterface | 
  ? {$_.AddressFamily -eq 'IPV4' -and $_.InterfaceAlias -notlike 'Loopback*'} | 
  Select IFIndex,InterfaceAlias,ConnectionState | 
  # Ask the user to select the interface
  Out-GridView -Title 'Please select the correct interface' -OutputMode Single

Set-NetIPInterface -InterfaceIndex $Selected.ifIndex -AddressFamily IPv4  ## etc...

That said, just setting up DHCP to handle this will almost always be easier. DHCP reservations are generally just as good as static IPs for user machines

  • Related