Skip to content

Instantly share code, notes, and snippets.

@ducke
Created October 6, 2015 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ducke/f6e42399930b15ddf2a7 to your computer and use it in GitHub Desktop.
Save ducke/f6e42399930b15ddf2a7 to your computer and use it in GitHub Desktop.
$IP = '192.168.138.10'
$MaskBits = 24 # This means subnet mask = 255.255.255.0
$Gateway = '192.168.138.2'
$Dns = '192.168.138.2'
$IPType = 'IPv4'
# Retrieve the network adapter that you want to configure
$adapter = Get-NetAdapter | Where-Object {$_.Status -eq 'up'}
# Remove any existing IP, gateway from our ipv4 adapter
If (($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress) {
$adapter | Remove-NetIPAddress -AddressFamily $IPType -Confirm:$false
}
If (($adapter | Get-NetIPConfiguration).Ipv4DefaultGateway) {
$adapter | Remove-NetRoute -AddressFamily $IPType -Confirm:$false
}
# Configure the IP address and default gateway
$adapter | New-NetIPAddress `
-AddressFamily $IPType `
-IPAddress $IP `
-PrefixLength $MaskBits `
-DefaultGateway $Gateway
# Configure the DNS client server IP addresses
$adapter | Set-DnsClientServerAddress -ServerAddresses $DNS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment