Learn about Windows PowerShell
Summary: Guest blogger, Gershon Levitz, talks about modifying DNS settings with Windows PowerShell.
Microsoft Scripting Guy, Ed Wilson, is here. Today we have a new guest blogger, Gershon Levitz. Gershon is a senior technical writer for Hyper-V at Microsoft.
The floor is yours, Gershon...
There are times when I need to change the DNS settings and use specific DNS servers. Then after I am done, I want to go back and use the DNS settings provided by DHCP. I can make these changes manually, but it would require a number of manual steps. Because I needed to do this on a regular basis, I investigated how to automate this by using Windows PowerShell.
Being fairly new to Windows PowerShell, I thought this was going to be a simple exercise—similar to writing a batch script. The actual Windows PowerShell cmdlets are fairly simple. However, I still needed to figure out how to run a Windows PowerShell script (ExecutionPolicy) and how to create a shortcut to the script file so that I could double-click an icon to run the scripts.
I created two simple Windows PowerShell scripts, one to modify the DNS settings to use specific DNS servers and another to set the DNS settings to automatic. In this blog post, I will walk you through the steps to create these scripts, and the steps needed to run the scripts from shortcuts without having to open a Windows PowerShell session.
The first thing you need to find out is the name of the adapter that you want to change. To list the network adapters, run the Get-NetAdapter Windows PowerShell cmdlet. In my case, the adapter name that I want to modify is Wi-Fi.
This script will set the Wi-Fi adapter to use the following DNS servers:
If the adapter is set to get its IP address automatically from a DHCP server, the cmdlet only overrides the DNS settings that are received from the DHCP server, and the adapter will continue to receive its IP address from the DHCP server.
Note You need to run the following command from an elevated command prompt.
Notice that the value for the –InterfaceAlias parameter is the name of the adapter from the previous image. Replace 184.108.40.206 and 220.127.116.11 with the IP addresses of the DNS servers you want to use.
Set-DnsClientServerAddress -InterfaceAlias Wi-Fi -ServerAddresses "18.104.22.168","22.214.171.124"
After running the command, run GetNetIPConfiguration to confirm the change to the DNS servers.
Or you can use the Get-DnsClientServerAddress cmdlet.
After you have the syntax of the command, and it works as you want it to, copy the command to a file with an extension of .ps1, for example DNSCustom.ps1.
This script will set the DNS setting to automatic for the network adapter:
Set-DnsClientServerAddress -InterfaceAlias wi-fi -ResetServerAddresses
After running the command, run Get-DnsClientServerAddress to confirm the change to the DNS servers. When the DNS setting is set to automatic, the ServerAddress column is blank for the adapter.
After you have the syntax of the command, and it works as you want it to, copy the command to a file with an extension of .ps1, for example DNSDefault.ps1.
To run Windows PowerShell scripts, you need to change the script execution policy.
For more information about the Windows PowerShell script execution policy, see:
To see the current policy, run the Get-ExecutionPolicy cmdlet.
To run each script file (.ps1) that you created, you can create a shortcut with the following command:
%systemroot%\System32\WindowsPowerShell\v1.0\powershell.exe –file “path to the script file”
For example, if I saved my scripts to D:\scripts, I would create two shortcuts—one for each script file with the following locations:
Note If you have spaces in the path to the file, enclose the path in quotation marks.
The last step is to set the shortcuts to Run as administrator:
Thank you, Gershon, for sharing your time and knowledge.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
Great one !!!
It's likely very obvious, but "...run GetNetIPConfiguration to confirm..." should be, "...Get-NetIPConfiguration..."
awesome, thank you