Network and Hostname Configuration using Powershell

This tutorial shows how to configure your network adapter and change the hostname using Powershell. For this tutorial a Windows Server 2016 Standard Edition server has been used without Desktop Experience.

Log in as Administrator. A Command Shell is shown. Type ‘powershell’ and hit enter.

Network Configuration

First you need to know which network adapters are available by using the Get-NetAdapter cmdlet. This will show all network adapter names, the interface index and the status for each network adapter.

Example:

Get-NetAdapter | ft Name,ifIndex,Status

Remember the ifIndex (InterfaceIndex) value for the network adapter that you want to configure.

 

If DHCP is already available on the network, the network adapter may already have an ip-address. To show the ipv4 ip-address for a specific network adapter, use the Get-NetIPAddress cmdlet. (You may use a different interface index value.)

Example:

Get-NetIPAddress -InterfaceIndex 4 -AddressFamily ipv4

 

To check all routes, including the default gateway, use the Get-NetRoute cmdlet.

Get-NetRoute -InterfaceIndex 4

To check the default gateway only:

Get-NetRoute -InterfaceIndex 4 -DestinationPrefix 0.0.0.0/0

 

To set the ip-address for the network adapter, use the New-NetIPAddress cmdlet. For example, add ip-address 192.168.20.10 with subnetmask 255.255.255.0 (prefixlength 24) and default gateway 192.168.20.254.

Example:

New-NetIPAddress -InterfaceIndex 4 -IPAddress 192.168.20.10 -PrefixLength 24 -DefaultGateway 192.168.20.254

To verify that the ip-address has been added correctly, use the Get-NetIPAddress cmdlet again.

 

To set the DNS ip-addresses, use the Set-DNSClientServerAddress cmdlet. Use a comma to separate multiple DNS ip-addresses.

Example:

Set-DNSClientServerAddress -InterfaceIndex 4 -ServerAddresses 192.168.20.10

 

Hostname Configuration

To check the current hostname, use the ‘env’ variable.

$env:COMPUTERNAME

 

To change the current hostname, use the Rename-Computer cmdlet. For example, change the computername to ADDS01.

Example:

Rename-Computer ADDS01

A reboot is required!

2 thoughts on “Network and Hostname Configuration using Powershell

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.