Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

PowerShell – Set DHCP DNS or Set Static DNS

Posted on January 16, 2018February 12, 2019 By David Kittell
param(
[string]$1,
[string]$2,
[string]$3
)

function Set-DNS([string]$EnableDHCP,[string]$dns1, [string]$dns2){
$saNetAdapters = Get-NetIPInterface | Where-Object {$_.ConnectionState -eq "Connected"} 

foreach ($sNetAdapter in $saNetAdapters.ifIndex)
{
    if ($EnableDHCP -eq "eDHCP")
    {
   
       Set-DnsClientServerAddress -InterfaceIndex $sNetAdapter -ResetServerAddresses
    }
    else
    {
    Set-DnsClientServerAddress -InterfaceIndex $sNetAdapter -ServerAddresses ($dns1, $dns2)
    }
}
Clear-DnsClientCache
}

function Get-NetworkInformation(){
$computer = $env:computername

try {
    $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer -EA Stop | ? {$_.IPEnabled}
   } catch {
        Write-Warning "Error occurred while querying $computer."
        Continue
   }
   $array = @()
   foreach ($Network in $Networks) {
    $NetworkAdapter = $Network.Description
    $IPAddress  = $Network.IpAddress[0]
    $SubnetMask  = $Network.IPSubnet[0]
    $DefaultGateway = $Network.DefaultIPGateway
    $DNSServers  = $Network.DNSServerSearchOrder
    $WINS1 = $Network.WINSPrimaryServer
    $WINS2 = $Network.WINSSecondaryServer   
    $WINS = @($WINS1,$WINS2)         
    $IsDHCPEnabled = $false
    If($network.DHCPEnabled) {
     $IsDHCPEnabled = $true
    }
    $MACAddress  = $Network.MACAddress
    $OutputObj  = New-Object -Type PSObject
    #$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
    $OutputObj | Add-Member -MemberType NoteProperty -Name NetAdapter -Value $NetworkAdapter
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
    $OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
    $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value ($DefaultGateway -join ",")      
    #$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
    $OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value ($DNSServers -join ",")     
    #$OutputObj | Add-Member -MemberType NoteProperty -Name WINSServers -Value ($WINS -join ",")        
    $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
    $array += $OutputObj
   }
   $array | Format-Table -AutoSize
}

clear

Write-Output "Current Network Information"
Get-NetworkInformation

# Enable DHCP DNS
#Set-DNS "eDHCP"

# Set Static DNS
#Set-DNS "dDHCP" "168.63.129.16" "168.62.167.9"

Set-DNS "$1" "$2" "$3"

Write-Output "Updated Network Information"
Get-NetworkInformation

Execute with

.\ChangeDNS.ps1 "eDHCP"
.\ChangeDNS.ps1 "dDHCP" "8.8.8.8" "8.8.4.4"
Originally Posted on January 16, 2018
Last Updated on February 12, 2019
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Azure Code DNS PowerShell Windows

Post navigation

Previous post
Next post

Related Posts

NetStat – Get Network Statistics

Posted on May 16, 2016June 16, 2020

sudo lsof -i # Get all network traffic information sudo lsof -i |grep *:http # Get all network traffic information using port 80 HTTP netstat -tulpn # Get all network traffic information netstat -tulpn | grep :80 # Get all network traffic information using port 80 HTTP netstat -o #…

Read More

PowerShell – Ping Subnet

Posted on November 9, 2015

#requires -Version 2.0 Function Ping-Subnet { #.Synopsis # Ping a subnet returning all alive hosts. #.Example # Ping-Subnet -IP 192.168.1.0 -Netmask /24 #.Example # Ping-Subnet -IP 192.168.1.128 -Netmask 255.255.255.128 Param( [string] $IP, [string] $netmask ) Begin { $IPs = New-Object System.Collections.ArrayList $Jobs = New-Object System.Collections.ArrayList $max = 50 Function Get-NetworkAddress…

Read More

Count/Limit characters in a textbox using Javascript

Posted on July 15, 2013October 26, 2015

<html> <head> <script type="text/javascript" src="/js/jquery-1.9.1.js"></script> <script type="text/javascript" src="/js/jquery.limit2.js"></script> <script type="text/javascript"> $(document).ready(function() { $("#text").limita({ limit: 100, id_result: "counter", alertClass: "alert" }); }); </script> </head> <body> <form id="input_form" method="POST" action="?"> <textarea id="text" rows="4" cols="49"></textarea> <input type="submit" value="submit"> </form> <div id="counter"></div> </body> </html> (function($){ $.fn.limita = function(options) { var defaults = { limit:…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes