{"id":2126,"date":"2014-01-26T00:01:00","date_gmt":"2014-01-26T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/01\/26\/weekend-scripter-modify-dns-settings-via-windows-powershell\/"},"modified":"2014-01-26T00:01:00","modified_gmt":"2014-01-26T00:01:00","slug":"weekend-scripter-modify-dns-settings-via-windows-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-modify-dns-settings-via-windows-powershell\/","title":{"rendered":"Weekend Scripter: Modify DNS Settings via Windows PowerShell"},"content":{"rendered":"<p><b>Summary<\/b>: Guest blogger, Gershon Levitz, talks about modifying DNS settings with Windows PowerShell.<\/p>\n<p>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.<\/p>\n<p>The floor is yours, Gershon&#8230;<\/p>\n<p>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.<\/p>\n<p>Being fairly new to Windows PowerShell, I thought this was going to be a simple exercise&mdash;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 (<b>ExecutionPolicy<\/b>) and how to create a shortcut to the script file so that I could double-click an icon to run the scripts.<\/p>\n<p>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.<\/p>\n<h2><b>Identify the name of the adapter<\/b><\/h2>\n<p>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 <b>Get-NetAdapter<\/b> Windows PowerShell cmdlet. In my case, the adapter name that I want to modify is <b>Wi-Fi<\/b>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-1-26-14-1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-1-26-14-1.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Script #1: Modify DNS server settings<\/h2>\n<p>This script will set the <b>Wi-Fi<\/b> adapter to use the following DNS servers:<\/p>\n<ul>\n<li>DNS Server 1: <b>1.1.1.1<\/b><\/li>\n<li>DNS Server 2: <b>2.2.2.2<\/b><\/li>\n<\/ul>\n<p>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.<\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> You need to run the following command from an elevated command prompt.<\/p>\n<p>Notice that the value for the <b>&ndash;InterfaceAlias<\/b> parameter is the name of the adapter from the previous image. Replace 1.1.1.1 and 2.2.2.2 with the IP addresses of the DNS servers you want to use.<\/p>\n<p style=\"margin-left:30px\">Set-DnsClientServerAddress -InterfaceAlias Wi-Fi -ServerAddresses &quot;1.1.1.1&quot;,&quot;2.2.2.2&quot;<\/p>\n<p>After running the command, run <b>GetNetIPConfiguration<\/b> to confirm the change to the DNS servers.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-1-26-14-2.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-1-26-14-2.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Or you can use the <b>Get-DnsClientServerAddress<\/b> cmdlet.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-1-26-14-3.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-1-26-14-3.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>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 <i>DNSCustom.ps1<\/i>.<\/p>\n<h2>Script #2: Automatically get DNS setting from DHCP server<\/h2>\n<p>This script will set the DNS setting to automatic for the network adapter:<\/p>\n<p style=\"margin-left:30px\">Set-DnsClientServerAddress -InterfaceAlias wi-fi -ResetServerAddresses<\/p>\n<p>After running the command, run <b>Get-DnsClientServerAddress<\/b><i> <\/i>to confirm the change to the DNS servers. When the DNS setting is set to automatic, the <b>ServerAddress<\/b> column is blank for the adapter.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-1-26-14-4.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-1-26-14-4.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>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 <i>DNSDefault.ps1<\/i>.<\/p>\n<h2>Allow Windows PowerShell scripts to run<\/h2>\n<p>To run Windows PowerShell scripts, you need to change the script execution policy.<\/p>\n<p>For more information about the Windows PowerShell script execution policy, see:<\/p>\n<ul>\n<li><a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/hh849812.aspx\" target=\"_blank\">Set-ExecutionPolicy<\/a><\/li>\n<li><a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/hh847748.aspx\" target=\"_blank\">About Execution Policies<\/a><\/li>\n<\/ul>\n<p>To see the current policy, run the <b>Get-ExecutionPolicy<\/b> cmdlet.<\/p>\n<h2>Create shortcuts to each file<\/h2>\n<p>To run each script file (.ps1) that you created, you can create a shortcut with the following command:<\/p>\n<p style=\"margin-left:30px\">%systemroot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe &ndash;file &ldquo;path to the script file&rdquo;<\/p>\n<p>For example, if I saved my scripts to D:\\scripts, I would create two shortcuts&mdash;one for each script file with the following locations:<\/p>\n<ul>\n<li>%systemroot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe &ndash;file d:\\scripts\\DNSCustom.ps1<\/li>\n<li>%systemroot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe &ndash;file d:\\scripts\\DNSDefault.ps1<\/li>\n<\/ul>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-1-26-14-5.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-1-26-14-5.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p style=\"margin-left:30px\"><b>Note<\/b>&nbsp; If you have spaces in the path to the file, enclose the path in quotation marks.<\/p>\n<p>The last step is to set the shortcuts to <b>Run as administrator<\/b>:&nbsp;<\/p>\n<ul>\n<li>Right-click the shortcut, and select <b>Properties<\/b>.<\/li>\n<li>On the <b>Shortcut<\/b> tab, click <b>Advanced<\/b>.<\/li>\n<li>On the <b>Advanced Properties<\/b> page, select <b>Run as administrator<\/b>.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-1-26-14-6.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/wes-1-26-14-6.png\" alt=\"Image of dialog box\" title=\"Image of dialog box\" \/><\/a><\/p>\n<p>Thank you, Gershon, for sharing your time and knowledge.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230; There are times when I need to change the DNS settings [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[480,56,37,3,298,61,45],"class_list":["post-2126","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-gershon-levitz","tag-guest-blogger","tag-networking","tag-scripting-guy","tag-tcpip","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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&#8230; There are times when I need to change the DNS settings [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=2126"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2126\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=2126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}