In this article, I’ll show you how to configure a static IP address on a system that has one network card through PowerShell. This can be useful when you have many servers and you would like to automate the IP addressing.
Below a simple example that will illustrating how you assign a IP address, gateway, and DNS server to a network adapter. The script lists all active network adapters, and when you select one network card and click on OK it uses the hard-coded addresses to assign the new values.
$NewIP = '192.168.2.109'
$NewGateway = '192.168.2.254'
$NewDNS = '184.108.40.206'
$Prefix = 24
$Adapter = Get-NetAdapter |
Where-Object Status -eq 'Up' |
Out-GridView -Title 'Select Adapter to Configure' -OutputMode Single
$Index = $Adapter.InterfaceIndex
New-NetIPAddress -InterfaceIndex $Index -IPAddress $NewIP -DefaultGateway $NewGateway -PrefixLength $Prefix -AddressFamily IPv4 -WhatIf
Set-DNSClientServerAddress –InterfaceIndex $Index –ServerAddresses $NewDNS -WhatIf
That is it. As you have seen this is a very simple and quick way to change IP settings for a network adapter using PowerShell.
I really like to hear what you have to say about this article:
- Was this article helpful for you or do you have any questions?
- Do you have additions, suggestions or any other ideas?
- Just let me know by leaving a comment below.
Thank you for spending some time at my site and in my blog. I hope you come to visit again soon!