Skip to content

Setup Static IP

This guide involves a PowerShell Static IP script that helps you configure your networking, including..

  1. Static IP
  2. Prefix Length
  3. Default Gateway
  4. DNS

The PowerShell script

Run the following script to assign a static IP to your machine

Change the script values

There are some variables you have to modify, such as InterfaceAlias, PrefixLength, DefaultGateway and DNSServers

$notepad SetStaticIP.ps1
# Prompt user for configuration details
$InterfaceAlias = "Ethernet"        # FOR ACCEPTING AS USER INPUT: Read-Host "Enter the Interface Alias (e.g., Ethernet)"
$PrefixLength = "26"                # FOR ACCEPTING AS USER INPUT: Read-Host "Enter the Prefix Length (e.g., 26)"
$DefaultGateway = "130.208.246.129" # FOR ACCEPTING AS USER INPUT: Read-Host "Enter the Default Gateway (e.g., 130.208.246.129, can be found in the documentation)"

############   For the Domain connecting to the Forest   ############
## You need to set the IP of the Forest as the DNS for your domain ##
#####################################################################

$DNSServers = "8.8.8.8,8.8.4.4"     # FOR ACCEPTING AS USER INPUT: Read-Host "Enter the DNS Servers (comma-separated if multiple, e.g., 8.8.8.8,8.8.4.4)"

$IPType = "IPv4"
$IPAddress = Read-Host "Enter the wanted static IP Address (e.g., 130.208.246.130 - x.x.x.149)"

# Retrieve the network adapter that you want to configure
$adapter = Get-NetAdapter -Name $InterfaceAlias

# Get the current network configuration
$currentConfig = Get-NetIPConfiguration -InterfaceAlias $InterfaceAlias

# Display current PrefixLength and Default Gateway
$currentPrefixLength = $currentConfig.IPv4Address.PrefixLength
$currentGateway = $currentConfig.IPv4DefaultGateway.NextHop
$currentIPAddress = $currentConfig.IPv4Address.IPAddress

Write-Output "Current PrefixLength: $currentPrefixLength"
Write-Output "Current Default Gateway: $currentGateway"
Write-Output "Current IP Address: $currentIPAddress"
Write-Output "Current DNS Servers: $($currentDNSServers -join ", ")"

# Check if there is already a static IP assigned
if ($currentConfig.IPv4Address.AddressState -eq "Preferred") {
    Write-Output "Static IP already assigned on $currentIPAddress"
}

# Always overwrite the existing IP address
Write-Output "Overwriting IP address with $IPAddress"

# Remove existing IP address if it exists
if ($currentIPAddress) {
    Remove-NetIPAddress -InterfaceAlias $InterfaceAlias -AddressFamily $IPType -Confirm:$false
    Start-Sleep -Seconds 5 # Pause to ensure the removal process completes
}

# Remove existing default gateway if it exists
if ($currentGateway) {
    Remove-NetRoute -NextHop $currentGateway -InterfaceAlias $InterfaceAlias -AddressFamily $IPType -Confirm:$false
    Start-Sleep -Seconds 5 # Pause to ensure the removal process completes
}

# Assign new static IP address and default gateway
$adapter | New-NetIPAddress `
    -AddressFamily $IPType `
    -IPAddress $IPAddress `
    -PrefixLength $PrefixLength `
    -DefaultGateway $DefaultGateway

# Set DNS server addresses
$DNSServersArray = $DNSServers.Split(",")
$currentDNSServers = $currentConfig.DnsServer.ServerAddresses
if ($currentDNSServers -ne $DNSServersArray) {
    $adapter | Set-DnsClientServerAddress -ServerAddresses $DNSServersArray
}

# Display the current network configuration for verification
Get-NetIPConfiguration -InterfaceAlias $InterfaceAlias