- Use Azure Bastion as a jump host for RDP and SSH - Tue, Apr 18 2023
- Azure Virtual Desktop: Getting started - Fri, Apr 14 2023
- Understanding Azure service accounts - Fri, Mar 31 2023
I’ve said it before, and I’ll say it again: if you want to “future-proof” your career as a Windows systems administrator, you need to develop sharpness with the following technologies:
- IPv6
- Windows PowerShell
Today’s topic focuses on the second bullet point with a nod to the first, if that makes sense. In other words, I’m going to give you an easy-to-follow tutorial on how you can configure you system’s network interface card (NIC) by using PowerShell version 3.
Starting PowerShell
Windows Server 2012 and Windows 8 come with PowerShell v3 already installed. If you run Windows Server 2008 or Windows 7, then you can install PowerShell v3 by downloading and installing the Windows Management Framework 3.0. In addition to upgrading PowerShell, the WMF 3.0 gives you updates to Windows Management Instrumentation (WMI) and Windows Remote Management (WinRM), among other technologies.
In this demo I’ll be using Windows 8. Let’s begin by opening an elevated command prompt and starting PowerShell.
Press the Windows key to transition from the Desktop environment to the Start Screen. Type cmd to run a quick search for the Windows Command Processor. Right-click the cmd icon and then click Run as Administrator. You’ll be jetted back to the Desktop and, after dealing with User Account Control (UAC), you’ll have an administrative command prompt session.
Starting an elevated command prompt in Windows 8
To start PowerShell, simply type powershell and press ENTER.
We can verify that we are running PowerShell 3.0 by running the PowerShell method $host.version.
PowerShell 3 ships with a minimal online help system by default. This is actually by design; you and I both know that the most current help documentation exists at Microsoft, not on our local file system.
Thus, I suggest that you periodically run update-help to download the full PowerShell help files, in addition to any updates that may be available.
Let’s now turn our attention to NIC configuration proper. I’ll try to inject this part of the demo with useful PowerShell tips and tricks that should prove useful for beginners.
NIC configuration with get-netadapter
Windows 8 and Windows Server 2012 automatically load PowerShell modules when you issue cmdlets. Thus, you don’t need to run import-module netadapter to make the NIC-related PowerShell available to your current PowerShell session.
Run the cmdlet get-netadapter (cmdlets are not case sensitive, by the way) to list all wired and wireless NICs on the local system.
Get-NetAdapter cmdlet output
Study the preceding screenshot if you would. You’ll want to note the friendly names of your NICs, because we actually most commonly reference them by name instead of interface index.
To that point, I can run get-netadapter –name ethernet | format-list * to get a run of all known properties of my NIC named “Ethernet.” In that cmdlet, name is a parameter of the get-netadapter cmdlet. We pipe (or redirect) the output from the get-netadapter cmdlet into the format-list cmdlet, which retrieves all properties and presents a nice list view for your studying pleasure.
Obtaining NIC properties
If you want to see every conceivable property of your NIC, run the cmdlet get-netadapteradvancedproperty Ethernet, substituting your adapter’s friendly name for the “Ethernet” used in my example.
Back to Figure 2, now. Did you notice that my second NIC has the name “Ethernet 2”? For easier reference through PowerShell, we should rename the friendly name of that NIC:
PS:>rename-netadapter –name “Ethernet 2” Ethernet2
Observe that if your NIC friendly name includes spaces, you need to enclose the name in quotation marks.
Let’s complete this little tour of PowerShell NIC configuration by disabling DHCP on my Ethernet adapter and providing a static IP configuration.
Consider the following PowerShell statement:
PS:>$netadapter = get-netadapter –name ethernet
The preceding code stores my Ethernet NIC as a variable named $netadapter. Now that we have the NIC data stored in memory, we can manipulate those values by issuing additional cmdlets.
We can disable DHCP on my interface by running $netadapter | set-netipinterface –dhcp disabled
The previous statement pipes our NIC configuration into the set-netipinterface cmdlet, setting the DHCP option to Disabled.
We use the new-netipaddress cmdlet to set a static IPv4 or IPv6 address to our interface. Let’s retrieve the full help to assist us in syntax and usage:
PS:>get-help new-netipaddress –full
Let’s continue to use our $netadapter variable, this time to set an IP address, subnet mask (called a prefix length), and a default gateway:
PS:>$netadapter | new-netipaddress –addressfamily IPv4 –ipaddress 192.168.1.55 –prefixlength 24 –type unicast –defaultgateway 192.168.1.1
Finally, we can assign a primary DNS server address to my Ethernet NIC:
PS:>$netadapter | set-dnsclientserveraddress –interfacealias Ethernet –serveraddresses 192.168.1.11
There you have it! We successfully configured my NIC by using only PowerShell v3 cmdlets.
Conclusion
I hope that you found this tutorial useful. I’ll leave you with a list of hand-picked resources that you are welcome to use for further study.
- Network Adapter Cmdlets in Windows PowerShell
- PowerShell Network Adapter Configuration Module
- Using PowerShell for NIC Configuration Tasks
- Use PowerShell to Configure the NIC on Windows Server 2012
- Using PowerShell to Get or Set Network Adapter Configuration
Hello – I have been trying unsuccessfully to find a way to use PS to set an IPAddress. Thank you for your instructions, but I cannot disable DHCP even though the command is accepted. I am running Windows 2012R2. I am running PS as an adminstrator. DHCP will not disable.
Any ideas?
Hello again – I discovered that I am able to control DHCP and to set an ipaddress IF the NIC is connected – NOT if it is unconnected.
I think I’m good – unless you have a comment on that.
Thank you again for your instructions !
– Allen
Hi Allen, I’m glad that you are sorted out. I’m glad you liked this article. Take care, Tim
Hi,
good article.
Do youn know a way to change index of adapter. I need to do this, and i can´t find out a way to achieve it.
Nice !!