Alan's sysadmin Blog

Working smarter not harder

Change default gateway on clients with static IP’s using PowerShell

Posted by Alan McBurney on July 29, 2010


Recently when working on a project I need to change the gateway on a bunch of clients that had static IPs.

This PowerShell Script did the job

——-Begin Script———-

function Set-Gateway  {

#Get NICS via WMI

$NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $_ -Filter “IPEnabled=TRUE”

foreach($NIC in $NICs) {$Gateway = “192.168.0.254”

$NIC.SetGateways($Gateway)

}

}

function Get-FileName {

$computer = Read-Host “Please specify the location of the file containing the computer names”

return $computer

}

$f = Get-FileName

Get-Content $f | foreach {Set-Gateway}

——-End Script———-

2 Responses to “Change default gateway on clients with static IP’s using PowerShell”

  1. How would this work for remote computers? For instance, I have a couple dozen servers that I need to change the gateway. How would I go about making that happen from the console of one server?

    • Alan McBurney said

      This is the intended behaviour of the script, to allow you to change the gateway of numerous computers using a PS script.

      All you need to do is have a list of computers that you wish to edit in a txt file (One computername per line)

      Whn you run the script it will ask you for the location of the txt file that contains the computers that you wish to alter the gateway on.

Leave a comment