TransWikia.com

Changing IP addresses on multiple computers via Powershell?

Super User Asked by Clouder43 on December 18, 2021

I have an assignment at my work where we need to change the static IP addresses (not DNS) on several computers. First, is it possible to use a script in Powershell to do this or can I use AD with group policy? They really want some fast/automated way of executing this and I’m having a tough time finding a resource script for this request.

2 Answers

You didn't tell us how you'll be deciding on the static IP for each computer, but you can surely find ways to determine that. You could have a CSV file with the hostname and the IP to set and have PowerShell read that.

As Señor CMasMas said, you can use WMI to set the static IP. To give a more concrete answer, here is an example script in PowerShell:

$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"
$wmi.EnableStatic("10.0.0.15", "255.255.255.0")
$wmi.SetGateways("10.0.0.1", 1)
$wmi.SetDNSServerSearchOrder("10.0.0.100")

This (from here) allows you to set the static IP address, subnet mask, default gateway and DNS server.

Answered by Worthwelle on December 18, 2021

You can use the Windows Management Instrumentation (WMI) to accomplish this. Powershell is one of many ways to manipulate WMI so if you wanted to use c#, windows scripting host, or something else with WMI access.. they all work the same.

You will first use a query to obtain the correct WMI object that allows you to tweak IP settings. You will then use the object you obtained to do so.

The object in question is win32_NetworkAdapterConfiguration.. this duder will give you access to the settings you desire. You will then call EnableStatic(IP, Netmask) on it and WELLA! (amazing.. right?)

A complete tutorial on this method can be found in two parts: Here for part 1 . And here for part 2 .

The first part of the tutorials above use PowerShell native cmdlets. The second part uses a WMI cmdlet (which I suggest). Either will work. The second will help you when you are using some other language.

HEY DUDE? (you might ask) WHERE ARE THE OPTIONS FOR THE MACHINE TO USE?? .. Look it up man.. there are many options.. I can't possibly tell you them all. Once you start using WMI for administration.. you will never go back. ;)

WMI will allow you to do everything from set a default printer to tell you who has what installed on their machine... you will become a windows administrative BADA$$ with this technology!

Need to put this in a script? Pseudo code..

foreach (computer in the network)
{
   get the wmi object for computer
   set to static ip on object
}

Answered by Señor CMasMas on December 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP