Batch Script Add Windows Users using Netsh

The following batch script will add a group of users to Windows, set no password, and require a password change at first logon.

@echo off
setlocal enabledelayedexpansion

set user1=John Q. Smith
set user2=Jane Doe
set user3=Joe Montana
set user4=Alicia Silverstone

set users=(user1 user2 user3 user4 user5 user6)

for %%u in %users% do (
  echo net user %%u /logonpasswordchg:yes /fullname:"!%%u!" /add
)

Use netsh to set interface IP static or dhcp

Get Configuration Info

Using the command below, you can gather information on the adapter’s current configuration. Make note of the connection name since that is what is used to in the configuration command further down.

netsh interface ip show config
Figure 1 - Example of netsh interface ip show config

Figure 1 – Example of netsh interface ip show config

You can see the existing configuration with the following command.

netsh interface ip dump
2016-05-05_113938

Figure 2 – Example of netsh interface ip dump

Set Interface DHCP

To set interface address as DHCP, issue the following.  Change the items highlighted in Red to suite your environment.

Note:  Use the above command (netsh interface ip dump) to figure out which interface you need to use.  Default is “Local Area Connection” but many environments will differ.  Match the interface= portion and supply that in the Red sections below.  I’m leaving the default of “Local Area Connection” but you can see that my interface would be “Ethernet”.

2016-05-05_113938-2

Figure 3 – Determine the interface name to use when configuring the interface via netsh

netsh interface ip set address "Local Area Connection" dhcp

You also can set the DNS statically or via DHCP as well.

netsh interface ip set dns "Local Area Connection" 8.8.4.4

Set a secondary DNS server:

netsh interface ip set dns "Local Area Connection" 8.8.4.4 index=2

Set Interface Static

Set the interface with a static address. Change the items in Red to suite your environment.

netsh interface ip set address "Local Area Connection" static ipaddr subnetmask gateway metric

Set the DNS server statically.

netsh interface ip set dns "Local Area Connection" 8.8.4.4

Set a secondary DNS server:

netsh interface ip set dns "Local Area Connection" 8.8.4.4 index=2

Tip: Change DNS Server for a Network Adapter via Command Line in Windows

Just a quick tip/note on changing the DNS server(s) for a network adapter in Windows from the command line.

To do this, you need elevated privileges on a command line (or you can use ‘runas’ from a non-elevated command line and provide administrative credentials).

c:>netsh interface ip set dns "Local Area Connection" static 8.8.8.8

To set it via DHCP you can just issue:

c:>netsh interface ip set dns "Local Area Connection" dhcp

Allow Pings to Windows XP using netsh

Here’s a quick note on allowing Pings (ICMP Echo Response) to your Windows XP from command line. You must be administrator.

netsh firewall set icmpsetting 8 enable

Quite simple.

If you’re not logged on as administrator and you bring up a normal command prompt, you can use:

runas /user:administrator netsh

Supply the administrator credentials and then when the netsh window pops up, you can set ICMP echo by just typing:

firewall set icmpsetting 8 enable

Allow Inbound ICMP on Server 2008 R2

Here is how to enable ICMPv4 echo from the command line using netsh in Server 2008 R2

netsh advfirewall firewall add rule name=ICMP Allow incoming V4 echo request protocol=icmpv4:8,any dir=in action=allow