≡ Menu

PowerShell: Assign static IP address

Assigning a static IP address is one common task that system administrators do after building a server. In today’s post I am providing a powershell function(Set-StaticIP) which takes IP address, subnet mask, default gateway and DNS servers as parameters and assigns it to the computer where you are executing the function.

function Set-StaticIP {
<# .Synopsis Assings a static IP address to local computer. .Description Assings a static IP address to local computer. .Parameter IPAddress IP Address that you want to assign to the local computer. .Parameter Subnetmask Subnet Mask that you want to assign to the computer. This is optional. If you don't specify anything, it defaults to "255.255.255.0". .Parameter DefaultGateway Default Gateway IP Address that you want to assign to the local computer. .Parameter DNSServers DNS servers list that you want to configure in network connection properties.     .Example Set-StaticIP -IPAddress 10.10.10.123 -DefaultGateay 10.10.10.100 -DNSServers 10.10.10.120,10.10.10.121             .Notes NAME:      Set-StaticIP AUTHOR:    Sitaram Pamarthi Website:   www.techibee.com #>
[CmdletBinding()]
param (            

        [alias('IP')]
        [Parameter(mandatory=$true)]
        [string]$IPAddress,            

        [alias('Sb')]
        [Parameter(mandatory=$false)]
        [string]$Subnetmask = "255.255.255.0",            

        [alias('Gateway')]
        [Parameter(mandatory=$true)]
        [string]$DefaultGateway,            

        [alias('DNS')]
        [parameter(mandatory=$false)]
        [array]$DNSservers            

)            

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPenabled=TRUE" | % {            

    Write-host "Trying to set IP Address on $($_.Description)"
    $_.EnableStatic($IPAddress,$Subnetmask) | out-null
    $_.SetGateways($DefaultGateway)  | out-null
    $_.SetDNSServerSearchOrder($DNSServers)  | out-null
}            

if($?) {
    write-host "IP Address set successfully"
} else {
    write-host "Static IP assignment failed"
}
}

Above simple function assigns the IP address and displays the output of execution.

Feel free to post here if you have any questions about the script.

Happy learning.

Comments on this entry are closed.

  • jack July 17, 2011, 11:27 pm

    helpful post

  • Johan August 9, 2011, 8:12 pm

    Cheers, just what I was looking for!

  • Vinnie Grack October 6, 2015, 2:43 pm

    I ran the command twice, however still no static IP assigned to host server? Any idea what I’m doing wrong here. I doesn’t seems to be working. Thanks Vinnie
    ———————————————————————————
    PS C:\Windows\system32> C:\Users\Administrator\Desktop\Assign Static IP.ps1

    PS C:\Windows\system32> ipconfig /all

    Windows IP Configuration

    Host Name . . . . . . . . . . . . : SOMOV
    Primary Dns Suffix . . . . . . . :
    Node Type . . . . . . . . . . . . : Hybrid
    IP Routing Enabled. . . . . . . . : No
    WINS Proxy Enabled. . . . . . . . : No
    DNS Suffix Search List. . . . . . : ABC.co.uk

    Ethernet adapter Ethernet:

    Connection-specific DNS Suffix . : ABC.co.uk
    Description . . . . . . . . . . . : Microsoft Hyper-V Network Adapter
    Physical Address. . . . . . . . . : 00-15-5D-02-D7-13
    DHCP Enabled. . . . . . . . . . . : Yes
    Autoconfiguration Enabled . . . . : Yes
    IPv4 Address. . . . . . . . . . . : 192.168.2.103(Preferred)
    Subnet Mask . . . . . . . . . . . : 255.255.255.0
    Lease Obtained. . . . . . . . . . : 06 October 2015 10:01:14
    Lease Expires . . . . . . . . . . : 14 October 2015 10:01:14
    Default Gateway . . . . . . . . . : 192.168.2.1
    DHCP Server . . . . . . . . . . . : 192.168.2.205
    DNS Servers . . . . . . . . . . . : 192.168.2.205
    192.168.3.205
    NetBIOS over Tcpip. . . . . . . . : Enabled

    Tunnel adapter isatap.ABC.co.uk:

    Media State . . . . . . . . . . . : Media disconnected
    Connection-specific DNS Suffix . : ABC.co.uk
    Description . . . . . . . . . . . : Microsoft ISATAP Adapter
    Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
    DHCP Enabled. . . . . . . . . . . : No
    Autoconfiguration Enabled . . . . : Yes

    Tunnel adapter Teredo Tunneling Pseudo-Interface:

    Media State . . . . . . . . . . . : Media disconnected
    Connection-specific DNS Suffix . :
    Description . . . . . . . . . . . : Teredo Tunneling Pseudo-Interface
    Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0
    DHCP Enabled. . . . . . . . . . . : No
    Autoconfiguration Enabled . . . . : Yes

    PS C:\Windows\system32> C:\Users\Administrator\Desktop\Assign Static IP.ps1

    PS C:\Windows\system32> Get-WmiObject -Class Win32_NetworkAdapterConfiguration

    DHCPEnabled : False
    IPAddress :
    DefaultIPGateway :
    DNSDomain :
    ServiceName : Rasl2tp
    Description : WAN Miniport (L2TP)
    Index : 0

    DHCPEnabled : False
    IPAddress :
    DefaultIPGateway :
    DNSDomain :
    ServiceName : RasSstp
    Description : WAN Miniport (SSTP)
    Index : 1

    DHCPEnabled : False
    IPAddress :
    DefaultIPGateway :
    DNSDomain :
    ServiceName : RasAgileVpn
    Description : WAN Miniport (IKEv2)
    Index : 2

    DHCPEnabled : False
    IPAddress :
    DefaultIPGateway :
    DNSDomain :
    ServiceName : PptpMiniport
    Description : WAN Miniport (PPTP)
    Index : 3

    DHCPEnabled : False
    IPAddress :
    DefaultIPGateway :
    DNSDomain :
    ServiceName : RasPppoe
    Description : WAN Miniport (PPPOE)
    Index : 4

    DHCPEnabled : False
    IPAddress :
    DefaultIPGateway :
    DNSDomain :
    ServiceName : NdisWan
    Description : WAN Miniport (IP)
    Index : 5

    DHCPEnabled : False
    IPAddress :
    DefaultIPGateway :
    DNSDomain :
    ServiceName : NdisWan
    Description : WAN Miniport (IPv6)
    Index : 6

    DHCPEnabled : False
    IPAddress :
    DefaultIPGateway :
    DNSDomain :
    ServiceName : NdisWan
    Description : WAN Miniport (Network Monitor)
    Index : 7

    DHCPEnabled : True
    IPAddress :
    DefaultIPGateway :
    DNSDomain :
    ServiceName : kdnic
    Description : Microsoft Kernel Debug Network Adapter
    Index : 8

    DHCPEnabled : False
    IPAddress :
    DefaultIPGateway :
    DNSDomain :
    ServiceName : AsyncMac
    Description : RAS Async Adapter
    Index : 9

    DHCPEnabled : True
    IPAddress : {192.168.2.103}
    DefaultIPGateway : {192.168.2.1}
    DNSDomain : ABC.co.uk
    ServiceName : netvsc
    Description : Microsoft Hyper-V Network Adapter
    Index : 10

    DHCPEnabled : False
    IPAddress :
    DefaultIPGateway :
    DNSDomain :
    ServiceName : tunnel
    Description : Microsoft ISATAP Adapter
    Index : 11

    DHCPEnabled : False
    IPAddress :
    DefaultIPGateway :
    DNSDomain :
    ServiceName : tunnel
    Description : Microsoft ISATAP Adapter
    Index : 12

    DHCPEnabled : False
    IPAddress :
    DefaultIPGateway :
    DNSDomain :
    ServiceName : tunnel
    Description : Microsoft Teredo Tunneling Adapter
    Index : 13
    ————————————————————-

    • TechiBee October 7, 2015, 7:17 am

      Hi, I don’t see that you are passing any arguments to script. Something like

      Set-StaticIP -IPAddress 10.10.10.20 -Subnetmask 255.255.255.0 -DefaultGateway 10.10.10.1