Powershell Script for Changing IP Settings on Multiple Servers from CSV file RRS feed

  • Question

  • Hi,

    Does anyone have a script that will change the IP, Subnet Mask, Gateway and DNS on multiple servers stored in a CSV file.

    I've tried this script but it seems to only work on the first server and is throwing back null object errors and connection errors. I plan to use the script to change the IPs which will cause them to lose network connectivity and then I will run a vmware script that will change the port groups of each server which will give them back network connectivity.

    $CSV = import-csv "C:\temp\IPInfo.csv"
    Foreach ($Item in $CSV)
    $Computer = $Item.ComputerName
    $IPaddress = $Item.IPAddress
    $Subnet = $Item.SubnetMask
    $GateWay = $Item.Gateway
    $DNS1 = $Item.Dns1
    $DNS2 = $Item.Dns2
    $NicAdapter = Get-WmiObject win32_networkadapterconfiguration -Computer $Computer -Filter "ipenabled = 'true'"
    $NicAdapter.EnableStatic($IPaddress, $Subnet)
    $NicAdapter.SetGateways($GateWay, 1)

    Thursday, June 13, 2019 8:59 AM

All replies

  • Remotely changing the IP on a server will cause the server to become unresponsive.  Any of those changes can disrupt the server, your connection and the network.

    In a modern domain you should not be using static settings.  DHCP can manage all servers with the exception of the DCs. 

    Changing the IP or Gateway remotely or locally will disable all current connections and the server will become unreachable. 


    Thursday, June 13, 2019 9:20 AM
  • Thanks, I am aware of what changing settings will do. I am just looking for a powershell script that will do this change. Downtime is part of this project. We do not use DHCP for our servers.


    Thursday, June 13, 2019 9:25 AM
  • The issue is not a scripting issue.  It is a network and systems issue.  Like they say - "you can't get blood from a stone".

    I recommend posting in the network forum for guidance on the best ways to accomplish your goal.


    Thursday, June 13, 2019 10:17 AM
  • Not really very helpful. The issue is a scripting issue as I'm simply looking for a script to change IP settings. If it causes the server to lose connection I don't care as it's part of my plan. All i will need to do is change the vm port group after the powershell script is ran using a power cli script i already have.
    Thursday, June 13, 2019 10:27 AM
  • I will suggest that you might get this to work by using PS remoting.

    Use "Invoke-Command" with a disconnected session. 


    Thursday, June 13, 2019 11:12 AM
  • This will eliminate the disconnect and the issues with the script finding more than one nic.

    $sb = {
        $nic = Get-WmiObject win32_networkadapterconfiguration -Filter "ipenabled = 'true'"
        if($nic.Count -gt 1){ 
            Throw 'too many nics'
            $nic.SetDNSServerSearchOrder(@($DNS1, $DNS2))
            $nic.EnableStatic($IPaddress, $Subnet)
            $nic.SetGateways($GateWay, 1)
    import-csv C:\temp\IPInfo.csv |
        $argList = @(
        Invoke-Command -ScriptBlock $sb -ComputerName $_.ComputerName -ArgumentList $arglist 

    If this fails then convert the "Invoke" to use a disconnected session.


    Thursday, June 13, 2019 11:25 AM