- PowerShell y CMD allow detailed network configuration in Windows
- Using scripts makes repetitive tasks easier and automates configurations.
- There are multiple commands and tools like Netsh, Get-NetIPConfiguration or New-NetIPAddress
- In addition to the IP, you can configure DNS, gateway and computer name
Setting a static IP address in Windows might seem like a task exclusive to the graphical environment, but it's also possible to do it quickly and efficiently from the command line. Both CMD and PowerShell offer powerful tools for modifying a computer's network settings without having to navigate through multiple menus in the graphical interface.
This capability is especially useful on servers, in environments without a graphical interface such as Server Core, or for remote administration tasks. Throughout this article, you will learn how to assign static IP addresses from CMD or PowerShell , as well as explore alternatives such as Netsh and Sconfig, including scripting and automation.
IP Configuration Using PowerShell
PowerShell allows you to make detailed and quick changes to your network configuration. To begin, it's essential to obtain the current information on available network cards. This is achieved with the following command:
Get-NetIPConfiguration
This command displays the current configuration of the network interfaces. To obtain specific information for each adapter, you can use:
Get-NetAdapter
This provides details such as the adapter name, connection speed, or MAC address.
To set a static IP, you need to use the cmdlet:
New-NetIPAddress -IPAddress 192.168.1.101 -InterfaceAlias 'Ethernet' -DefaultGateway 192.168.1.1 -AddressFamily IPv4 -PrefixLength 24
This command sets the IP 192.168.1.101 on the Ethernet adapter, with a mask of 255.255.255.0 and gateway 192.168.1.1.
If a previous configuration already exists, it can be deleted with:
Remove-NetIPAddress -IPAddress 192.168.1.101 -DefaultGateway 192.168.1.1
And to configure the DNS servers:
Set-DnsClientServerAddress -InterfaceIndex 12 -ServerAddresses ('8.8.8.8','1.1.1.1')
Using PowerShell scripts to automate
A very efficient way to avoid typing commands every time, especially useful in mass deployments, is to use PowerShell scripts. A basic example would be the following:
$direccionip = "192.168.1.101"
$prefijodered = "24"
$puertadeenlacegw = "192.168.1.1"
$dns = @("8.8.8.8", "1.1.1.1")
$interfaz = (Get-NetAdapter).ifIndex
New-NetIPAddress -IPAddress $ipaddress -PrefixLength $redprefix -InterfaceIndex $interface -DefaultGateway $gwgateway
Set-DnsClientServerAddress -InterfaceIndex $interface -ServerAddresses $dns
This script configures IP addresses and DNS servers in bulk, simplifying management across multiple computers. Additionally, you can include a computer name change in the script.
$nombre = "Servidor01"
Rename-Computer -NewName $nombre -force
To run this script, simply save it as .ps1 and run it from PowerShell with administrator privileges.
Process images
This procedure can be best visualized using graphical tools, but it is also possible to monitor it from PowerShell by entering commands such as:
Get-NetIPAddress
Test-Connection google.com
The first one shows configured IPs, while the second one performs a ping to check connectivity.
Configuration with Netsh from CMD or PowerShell
Netsh is a traditional and still useful tool for configuring the network via the command line.
To view the current interface configuration:
netsh interface ipv4 show config
To set a static IP:
netsh interface ipv4 set address name="Ethernet0" static 192.168.100.6 255.255.255.0 192.168.100.253
And to configure primary and secondary DNS:
netsh interface ipv4 set dns name="Ethernet0" static 1.1.1.1
netsh interface ipv4 set dns name="Ethernet0" static 8.8.8.8 index=2
Or using the more modern add command:
netsh interface ipv4 add dnsserver name="Ethernet0" address=1.1.1.1 index=1
netsh interface ipv4 add dnsserver name="Ethernet0" address=8.8.8.8 index=2
This method works in both traditional CMD and PowerShell, although Netsh may be less flexible than PowerShell for complex tasks.
Sconfig Utility for Server Core
Windows Server Core servers do not have a graphical interface, therefore, the sconfig tool accessible from CMD or PowerShell can be used:
Write sconfig and press Enter. An interactive menu opens with options to change network settings, computer name, join domains, and more.
To change the IP, select option 8 (Network Settings), choose the desired card and then press option 1. Choose Static IP(s) and fill in the requested data: IP address, subnet mask and gateway.
You can then configure DNS with option 2.
This method is very practical for quick server setups.
Using WMI and CIM for advanced configurations
Windows Management Instrumentation (WMI) and Common Information Model (CIM) are classes that allow you to manipulate configurations from PowerShell using detailed methods. These tools simplify complex tasks and enable deep network administration automation.
For example, to get all active IPs:
Get-CimInstance -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=$true | Select-Object -ExpandProperty IPAddress
To view all properties:
Get-CimInstance -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=$true | Format-List
You can also configure DNS:
$wql = 'SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=True'
$args = @{ DnsDomain = 'miempresa.com' }
Invoke-CimMethod -MethodName SetDNSDomain -Arguments $args -Query $wql
And to release or renew DHCP leases:
Invoke-CimMethod -ClassName Win32_NetworkAdapterConfiguration -MethodName ReleaseDHCPLeaseAll
Invoke-CimMethod -ClassName Win32_NetworkAdapterConfiguration -MethodName RenewDHCPLeaseAll
Classic CMD for IP configurations
On older computers or for users accustomed to the classic command prompt, you can use the netsh utility and other traditional commands. These commands remain useful for quick tasks and in environments where PowerShell is unavailable or undesirable.
ipconfig /all: to view all current settings.netsh interface ipv4 set address name="Ethernet" source=static address=192.168.1.10 gateway=192.168.1.1: to set IP and gateway.netsh interface ipv4 add dnsserver name="Ethernet" address=8.8.8.8 index=1: to add DNS.
Although less flexible than PowerShell, it is still very useful in basic scripting environments and in situations where compatibility is paramount.
Assigning a static IP address in Windows is an essential task in many professional environments, especially for servers and computers that require fixed addresses for specific services. As you've seen, you have multiple tools to accomplish this, from PowerShell and Netsh to the sconfig utility. While each method has its advantages, PowerShell stands out for its automation capabilities and depth of configuration. If you're managing a network or servers, taking the time to master these commands will save you a lot of headaches and facilitate remote administration, deployments, and environment standardization.
Passionate writer about the world of bytes and technology in general. I love sharing my knowledge through writing, and that's what I'll do on this blog, show you all the most interesting things about gadgets, software, hardware, tech trends, and more. My goal is to help you navigate the digital world in a simple and entertaining way.