# Change all default gateways

**URL:** <https://forums.powershell.org/t/change-all-default-gateways/5077>\
**Category:** PowerShell Help\
**Created:** [October 22, 2015, 1:28am UTC](https://forums.powershell.org/t/change-all-default-gateways/5077 "2015-10-22T01:28:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![37mm](https://avatars.discourse-cdn.com/v4/letter/3/48db29/32.png) [@37mm](https://forums.powershell.org/u/37mm)\
**Post date:** [October 22, 2015, 1:28am UTC](https://forums.powershell.org/t/change-all-default-gateways/5077/1 "2015-10-22T01:28:49Z")

</div>

I need to change the default gateways on about 50 workstations. I feel like I should be able to do this in power shell. Not really sure where to start.

$domainHost = Get-ADComputer -Filter \* -Properties \* | select CN

foreach ($item in $domainHost)  
{  
???  
}

I am postive it can be done. Not sure how to accomplish this. Can’t use DHCP to do this as we are locked in to statics for RDP. Any input is greatly appreciated, Cheers!

---

<div class="post-metadata">

**Author:** ![mark-stone](https://avatars.discourse-cdn.com/v4/letter/m/898d66/32.png) [@mark-stone](https://forums.powershell.org/u/mark-stone)\
**Post date:** [October 22, 2015, 3:05am UTC](https://forums.powershell.org/t/change-all-default-gateways/5077/2 "2015-10-22T03:05:30Z")

</div>

Try the following code  
$domainHost = Get-ADComputer -Filter \* -Properties CN | select CN  
foreach ($item in $domainHost)  
{  
$n2 = Get-WMIObject win32\_NetworkAdapterConfiguration -computer $[Item.CN](http://Item.CN) -filter “IPEnabled = True and DHCPEnabled = False” | where-object {$_.defaultIPGateway -eq ‘EXISTING GW IP’'}  
$n2 | ForEach-Object { write-output "$($[item.CN](http://item.CN)) $($_.IPaddress) $($_.IPSubnet) $($_.defaultIPGateWay)" }  
#$n2.SetGateways(“NEW GW IP”)  
}

Replace “EXISTING GW IP” and “NEW GW IP” with the proper IP addresses.

Also the operational line is commented out. I recommend you leave it commented and do a dry run first. You also may want to test it on one or two workstations prior to full rollout.

---

<div class="post-metadata">

**Author:** ![37mm](https://avatars.discourse-cdn.com/v4/letter/3/48db29/32.png) [@37mm](https://forums.powershell.org/u/37mm)\
**Post date:** [October 22, 2015, 6:59am UTC](https://forums.powershell.org/t/change-all-default-gateways/5077/3 "2015-10-22T06:59:21Z")

</div>

Ok, it runs fine but the gateway stays the same.

---

<div class="post-metadata">

**Author:** ![37mm](https://avatars.discourse-cdn.com/v4/letter/3/48db29/32.png) [@37mm](https://forums.powershell.org/u/37mm)\
**Post date:** [October 22, 2015, 7:07am UTC](https://forums.powershell.org/t/change-all-default-gateways/5077/4 "2015-10-22T07:07:24Z")

</div>

```
$n2 = Get-WMIObject win32_NetworkAdapterConfiguration -ComputerName $env:COMPUTERNAME -filter "IPEnabled = True and DHCPEnabled = False" | where-object {$_.defaultIPGateway -eq 'old Gateway'}
$n2 | ForEach { write-output "$($item.CN) $($_.IPaddress) $($_.IPSubnet) $($_.defaultIPGateWay)" }
$n2.SetGateways('MY GATEWAY')
```

---

<div class="post-metadata">

**Author:** ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)\
**Post date:** [October 22, 2015, 1:44pm UTC](https://forums.powershell.org/t/change-all-default-gateways/5077/5 "2015-10-22T13:44:01Z")

</div>

I have tested the code Mark suggested, and it works quite well against a Window 7 machine. There was one small type-o and I made a few adjustments to output a PSCustomObject rather than a string and a couple other tweeks, but the base code is essentially the same.

The only place where I ran into issues was when changing to a different default gateway that caused my workstation to no have network communication with the remote machine. In that situation, the WMI setgateways method does on get the completion message back because communications have dropped. WMI will sit there for a while waiting for the response before timing out.

```
$oldgateway = "EX. 1.1.1.1"
$newgateway = "EX. 1.1.1.2"

$domainHost = Get-ADComputer -Filter * -Properties CN | select CN
foreach ($item in $domainHost)
{
    $n2 = Get-WMIObject win32_NetworkAdapterConfiguration -computer $item.CN -filter "IPEnabled = True and DHCPEnabled = False" | where-object {$_.defaultIPGateway -eq $oldgateway}
    $n2 | ForEach-Object { [PSCustomObject]@{ ComputerName = $item.CN; IPAddress = $_.IPaddress; SubnetMask = $_.IPSubnet; Gateway = $_.defaultIPGateWay } }
# $n2.SetGateways($newgateway)
}
```

Again as Mark did, I have comment out the SetGateways to prevent accidentally crippling your environment, you will have to uncomment that if you are sure you want it to process.

---

<div class="post-metadata">

**Author:** ![37mm](https://avatars.discourse-cdn.com/v4/letter/3/48db29/32.png) [@37mm](https://forums.powershell.org/u/37mm)\
**Post date:** [October 22, 2015, 11:25pm UTC](https://forums.powershell.org/t/change-all-default-gateways/5077/6 "2015-10-22T23:25:27Z")

</div>

This worked awesome guys, Really saved me alot of time this morning. Powershell is the number one reason for my weight gain, lol , never have to get up!

---

<div class="post-metadata">

**Author:** ![milos-ostojic](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@milos-ostojic](https://forums.powershell.org/u/milos-ostojic)\
**Post date:** [November 27, 2015, 6:00am UTC](https://forums.powershell.org/t/change-all-default-gateways/5077/7 "2015-11-27T06:00:54Z")

</div>

Has anybody been using CIM commands instead of WMI? I’ve been struggling with type of DefaultIPGateway argument.  
Working on a project where changing gateways precedes configuring network switch routes (done by network department). So, no connection for a while. Been working with InDisconnectedSession parameter of Invoke-Command and Remove-NetRoute ; New-NetRoute. The best way I found to avoid being stuck forever with remoting.  
Still devastated by not being able to resolve mystery of setgateways cim method.  
BR

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:44pm UTC](https://forums.powershell.org/t/change-all-default-gateways/5077/8 "2024-05-16T20:44:22Z")

</div>

Closed on May 16, 2024, 8:44pm UTC


