Skip to main content
  1. Articles/

Set source IP for outbound traffic

·551 words·3 mins· loading
Rodrigo Silva
Rodrigo Silva
20+ years in IT. That’s a lot of years.
Table of Contents

If you work with IIS web servers, chances are that at some point you had to set the NIC’s primary IP for outbound traffic. By default, Windows will use the lowest numerical IP, and that is usually ok in most scenarios.

For example, if you have IPs, and, the network adapter will use as the default source IP for all outgoing connections. But what if you need it to be instead?

Some applications will give you the option to specify the source IP. The command ping, for example, has the parameter /S.

ping /S

But that’s more of an exception. Most applications won’t allow for a source IP to be specified. If you have firewall rules restricting access to specific IPs, you will need to make sure your NIC initiates connections with the correct IP.


Windows will allow you to overwrite the default lowest numerical IP with the -SkipAsSource parameter, but that is not really straightforward. By default, all IPs have that parameter set to $false so any IP could be used as source. If you want to specify a single IP to be the source, you need to manually set all the other IPs to $true. That is counterintuitive.

Set-NetIPAddress -IPAddress -SkipAsSource $true
Set-NetIPAddress -IPAddress -SkipAsSource $false
Set-NetIPAddress -IPAddress -SkipAsSource $true


I find myself having to deal with this kind of changes quite often, so I decided to write the Set-SourceIP script to simplify the process.

We are still using Set-NetIPAddress in the background along with the -SkipAsSource parameter, but in a more intuitive way. Instead of having to think about all the IPs that you don’t want to be the source, you just need to know the one you do.

This is how you would use the script to set as the source IP:

Set-SourceIP -IPAddress

When you run the script, it queries the NIC for all the IP addresses and sets their-SkipAsSource to $true. Then it gets the IP you specified in -IPAddress and sets it back to $false. It’s pretty simple, but effective.


I like to use familiar nomenclature when writing my scripts, so they look like an extension of Powershell. I usually name my scripts something like “Get-” or “Set-” in an attempt to mimic Powershell’s own commands. And whenever possible, I use the same name for the parameters.

    [string[]] $IPAddress,
    [switch] $Reset


The -IPAddresss parameter is used to specify the IP you want to be used as the source. It’s the same name that you see in Set-NetIPAddress. This is the part of the code that runs when you use it:

$IPs = Get-NetAdapter | Get-NetIPAddress | Select-Object -expand IPAddress

if ($IPAddress) {

    foreach ($IP in $IPs) {
        Set-NetIPAddress -IPAddress $IP -SkipAsSource $true

    Set-NetIPAddress -IPAddress $IPAddress -SkipAsSource $false


The $IPs variable is used to store all the IPs that were queried from the network adapter.


I have also included a -Reset parameter to easily set the adapter back to its default setting.

if ($Reset) {

    foreach ($IP in $IPs) {
        Set-NetIPAddress -IPAddress $IP -SkipAsSource $false


You can fork the final script on Github or install it from PowerShell Galery.

  Fork     Install