Windows Server 2012 R2 Network Cmdlets: Part 4

Windows Server 2012 R2 Network Cmdlets: Part 4

  • Comments 2
  • Likes

Summary: Use Windows PowerShell to work with IP addresses in Windows Server 2012 R2.

Hey, Scripting Guy! Question Hey, Scripting Guy!

Now that we have Windows PowerShell cmdlets to use throughout Windows Server 2012 R2, is there any easier way to assign IP addresses to network adapters?


Hey, Scripting Guy! Answer Hello EL,

Honorary Scripting Guy, Sean Kearney, is here today, unlocking more of the Power of the network cmdlets. This is the fourth part in a series called Windows PowerShell Network Week. You also might enjoy reading:

When I first tried script to set an IP address on a server, it was without good "fiend" (did I say fiend?) netsh.exe.

Actually netsh.exe wasn’t bad, but it was an older way of doing things in the old server world.

I didn’t like working that way because it just wasn’t that consistent. I would learn a little about DSQuery.exe, and then find that I had to relearn things just to use netsh.exe. So for that reason, I wasn’t very productive in the old word of command-line utilities. You had to master each individual utility and its own special syntax.

So with netsh.exe, you used to do something like this to set up your IP address:




Then quickly grab the pen and paper or Notepad and try to copy the names out of the window. “Good, my network adapter is called “Local Area Connection”.”

Then with that knowledge in mind, assign the IP address:

NETSH Interface>TCP

NETSH Interface tcp>IP

NETSH Interface ipv4>SET address name=”Local Area Connection” static

To be honest, after you had the pattern down, you could script a little line like this for the task:

NETSH interface ipv4 name=”Local Area Connection” static

But then you step into a new scenario with a server with a few network adapters, and perhaps you want to automate it a bit more. Netsh.exe could be automated, but not easily.

Now things have gotten a lot easier. The Windows Powershell cmdlets for creating an IP address just got way easier. First let’s think…with IPconfig, how would you get your IP address if you had a bunch of network adapters? I used to do something like this:

Ipconfig | FIND “IPv4”

That worked OK. I could at least get a list of the information. But what if I wanted to parse that data? Yes, you could do that in DOS and the old world—but not as easily as I would have liked.

In Windows PowerShell, we have the option to pull that information out as an object. This means parsing of that data is far easier to access. To get our IP addresses, we just use this cmdlet:


This will dump it out in a nice list or we can choose to view it in a table:

Get-NetIPAddress | Format-Table

Or if I so choose, I can take only what I want out of this list:

Get-NetIPAddress | Select-Object IPAddress, InterfaceAlias

If I need to create a new IP address to be used, I can use the New-NetIPAddress cmdlet. The biggest difference in using this cmdlet over using netsh.exe is that I plug in the number of bits for the subnet mask.

So first we can find our live network adapters. This is something that (as we saw last time in Windows Server 2012 R2 Network Cmdlets: Part 3) was far easier by using Windows PowerShell:

Get-NetAdapter –physical | Where { $_.Status –eq ‘Up’ }

Now if you’re just plugging in a cable, and you want to assign an IP address to the only live network adapter, you can pipe this output directly into New-IPAddress:

To assign the IP address of, a subnet mask (a 24-bit prefix), and a default gateway of, we could use the following command—and because it’s Windows PowerShell, we can even do it in a safe manner with –whatif:

Get-NetAdapter –physical | Where { $_.Status –eq ‘Up’ } | New-NetIPAddress –Ipaddress
–PrefixLength 24 –DefaultGateway –whatif

If you’re satisfied with the results, and you’re confident, you can “Release the Kraken” and get rid of the –whatif:

Get-NetAdapter –physical | Where { $_.Status –eq ‘Up’ } | New-NetIPAddress –Ipaddress
–PrefixLength 24 –DefaultGateway

Pretty cool eh? We now have an easier way to identify the live network adapters that we want to assign an IP address to on a new server.

EL, take a gander in tomorrow as we play with adding more interesting bits, like DNS settings, to those configurations!

I invite you to follow The Scripting Guys on Twitter and Facebook. If you have any questions, send an email to The Scripting Guys at, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then remember eat your cmdlets each and every day with a taste dash of creativity.

Sean Kearney, Windows PowerShell MVP, Honorary Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment