How To Set An IP Address To Static And DHCP With PowerShell

How to set an IP address requires a couple cmdlets and some know-how. In this video, Adam goes over changing the NIC properties to set an IP address to static or change it back to DHCP using PowerShell.

for this techsnip we're going to work on how to find an IP address on a Windows machine? How to set an IP address and how to remove it back. So we're going to work on how to manage that IP address so the first thing I want to do is I'm just going to set my IP address to a variable here and we will just do that an next. I want to go over the get netIPaddress command so they get netIPaddress command is the command that allows you to pull back all of the different information. On network adapters on your machine, so it pulls back all the information like ipv 6. IP's if you have those ipv 4. IP's loop back adapter's sometimes various Bluetooth adapters. All the different adapters that you have on your machine by default. It doesn't pull back very much useful information because there's just way too much of it. So the first thing you have to do is figure out how to get the information that you wanted to work with so to do that. I would typically go away over here to the gui and see that more obvious what you want to change so. I know that I don't want to change this 'cause as network cable unplugged. This is some tap adapter. Then I don't want to change my hyper V adapter. I want to change my Ethernet 2 adapter. So it's called Ethernet to when I go to the powerShell. Again, I can use netIPaddress. An I can narrow that down to ipv 4 addresses, which is good, but let's see how many I get here. I still have 4 entries, but I only have 3, there in the gui so I can get this down a little bit bye. Selecting only the interface alias and notice that I have for their so the loop back adapter isn't shown so it just fine. Alright so now that I know what the actual interface alias name is I can just choose that in here and just choose Ethernet 2. And it's only going to bring back the IP address that I currently have so I've just brought back. This one and right now is set to, so it doesn't DHCP isn't even working for some reason so let me first get this in a state where DHCP should be working, so let's just do this, let's disable it enable it. And we run this again and now the DHCP is working so that's great, so now. It shows that prefix. Origin and suffix origin as both DCP so we now is set to DCP. Alright so now they know how to get the configuration that we're looking for we now need to set the static IP address on it now there's actually 2 different commands to do this. There's new-netIPaddress and set-netIPaddress and you may think well. Why is there 2? Don't ask me I wish there was only one command but ask Microsoft? So when you're moving from DHCP to a static IP address. You have to use new-netIPaddress so let me show you what happens when you use set IPaddress instead so I just ran set IPaddress an it tells me that no matching MSFT netIPaddress objects found that's because it's only looking for static IP addresses this time and then notice that set-netIP address just has the interface alias parameter that were used before to define the network adapter. The IP address that we want to use and the prefix link of 24. Which is the subnet mask of 255.255.255 dot zero alright so now because we know we have a DHCP? We have to use new-netIPaddress so I will go ahead and run this and then notice that exact same sort of format, exact same parameters set night dress. But we have the default gateway. There and if we didn't return an error. We return the IP address object backs of information looks like it was successful to confirm. We can then go in here into the gui and then notice that our IP address and network information was created so now. Maybe we want to remove it, so we use the new command. You would think that the remove command would revert back all the changes that we did so I went ahead run. Remove netIPaddress. Yes, to all all right now, let's see what it looks like. So it removed the IP address and subnet mask, but the default gateway remain. Unfortunately, we have to use a different command called remove-netroute to remove the default gateway using the same interface alias parameter again. Referencing Ethernet 2 hour run remove-netroute hit yes. Then go back in here and confirm that it's gone. Everything is gone, but it's not set back to DHCP right now. The machine doesn't have any network connectivity at all. It's pretty much worthless at this point, so we have to change it back to DHCP to do that. We use a different command called set-netIPinterface and again. We're setting the interface alias to Ethernet 2 and then there is a deep parameter and with a required parameter argument of enabled so I can go ahead and run this and then going back to see what it looks like now. Now everything is back to DCP how we had it in the 1st place and everyone is happy, so that's how you create a static IP address remove a static IP address. You can change the static IP address with set netIPaddress and then finally how we can remove the default gateway and then set the NIC back to DHCP.