Interacting with TCP/IP Through PowerShell (Part 4)

If you would like to read the other parts in this article series please go to:

In my previous article, I showed you a somewhat cumbersome method for retrieving basic IP address information through PowerShell. Now that you know how to view the IP address configuration, I want to continue the discussion by showing you how to actually provision an IP address using PowerShell.

Before I get started, let me just reassure you that the technique that I am going to talk about in this article is easier than the one that I covered in my previous article. In the last article, I delved into some somewhat advanced techniques and I realize that some might have been put off by how much work was required to do something as simple as retrieving an IP address.

For the sake of demonstration, I have set up a Windows 8.1 machine and it is currently using a dynamically assigned IP address. If you look at Figure A, you can see that this virtual machine is currently using the address The default gateway and the DNS server are both set to You can also see that I retrieved this information by using the Get-NetIPConfiguration cmdlet.

Figure A: Right now this VM is using a dynamically assigned IP address.

As I am sure you already know, a computer (physical or virtual) can contain multiple network adapters. I mention this because the IP address is bound to a specific network adapter. In the interest of keeping things simple, my virtual machine only has a single virtual network adapter. Even so, we have to identify the network adapter (or virtual network adapter in this case) before we can assign an IP address to it.

The easiest way to identify a network adapter through PowerShell is by entering the Get-NetAdapter cmdlet. If you look at Figure B, you can see that my virtual machine has a single network adapter and it is named (conveniently enough) Ethernet. The virtual network adapter’s description is Microsoft Hyper-V Virtual Network Adapter.

Figure B: The virtual machine contains a single virtual network adapter named Ethernet.

We are going to have to reference this network adapter a couple of times as we configure the machine’s IP address. The easiest way to do this is to link the network adapter to a variable that will represent the network adapter. Fortunately, PowerShell makes it really easy to declare a variable. You just have to choose a name for the variable. Once you have picked out the variable name, specify a dollar sign before the name and an equal sign after the name. For instance, if I wanted to use a variable named adapter I could assign it to the network adapter by using the following command:

$adapter = Get-NetAdapter -Name Ethernet

It is worth noting that the dollar sign becomes a part of the variable’s name. From here on out, the variable $adapter will represent our network adapter.

Now that we have linked a variable to our network adapter, we are almost ready to assign an IP address. Before we can do that though, there are a few pieces of information that we will need. Specifically, we will need to choose the new IP address, the prefix length, and the default gateway.

I am assuming that if you are reading this then you are familiar with the concept of an IP address and a default gateway, but I realize that the concept of a prefix length might seem foreign to some. The prefix length essentially takes the place of the subnet mask. Rather than specifying the subnet mask, we are going to specify the number of bits that will make up the IP address prefix.

So for the sake of demonstration, I am going to set the virtual machine’s IP address to I will use as the default gateway, and I will set the prefix length to 24.

We can assign their new values to the network adapter by using the New-NetIPAddress cmdlet. The cmdlet requires us to specify an interface alias. The interface alias is really just the name of our network adapter, as we are using a variable for that. We will also have to specify the IP address, the prefix length, and the default gateway. The other piece of information that we have to supply is the IP address family. Since we are working with IPv4 addresses, the address family is IPv4. The actual command looks like this:

New-NetIPAddress -InterfaceAlias $adapter.Name -AddressFamily IPv4 -IPAddress -PrefixLength 24 -DefaultGateway

There is a chance that when you run this command, you will receive an error message like the one that is shown in Figure C. As you can see in the figure, PowerShell is giving us an Access Denied message.

Figure C: You may receive an Access Denied error.

The reason for this error is that you will only be able to use PowerShell to change the IP address if you are using an elevated PowerShell session. To open an elevated PowerShell session, move your mouse pointer to the upper right portion of the screen (in Windows 8) and click on the Search charm. Enter PowerShell into the search box. When the search results are displayed, right click on the listing for PowerShell and choose the Run As Administrator option from the shortcut menu.

If you do forget to run PowerShell as an administrator and then have to go back and open an elevated window, you will have to re-declare the $adapter variable. Variables are session specific and do not carry over between PowerShell sessions. You can see what the command looks like when run in Figure D.

Figure D: We have changed the IP address from PowerShell.

We are almost done, but we have not yet made a DNS server assignment. To do so, enter the following command:

Set-DnsClientServerAddress -InterfaceAlias $adapter.Name -ServerAddresses

In this case, the ServerAddress parameter is used to specify the address of our DNS server. You can see what this looks like in action in Figure E. As you can see in the figure, I have used the Get-NetIPConfiguration cmdlet to verify the DNS server assignment.

Figure E: We are assigning a DNS server.

Given the length of some of the PowerShell commands that we have used, it is easy to make a typo. If you make a mistake, the command won’t usually work. Worse yet, the New-NetIPConfiguration cmdlet tends to return some really cryptic error messages. Fortunately, there are a couple of things that you can do when things go wrong.

First, try typing the name of the variable. You saw me do that in Figure D. Entering the variable name by itself allows you to confirm that the variable contains good data.

The other thing that you can do is to use the Get-NetIPConfiguration cmdlet to test the information that you are trying to enter into the New-NetIPConfiguration cmdlet. Both cmdlets use the same parameters. As such, you might type something like:

Get-NetIPConfiguration -InterfaceAlias $adapter.Name -AddressFamily IPv4

This will return the current IP address for the specified network adapter and will allow you to verify that a portion of the information that you are entering is correct.


As you can see, it is relatively straightforward to configure TCP/IP through PowerShell. The techniques that I have shown you tend to be most useful when you need to configure multiple machines.

If you would like to read the other parts in this article series please go to:

Leave a Comment

Your email address will not be published.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top