Setting a static DNS address on an adapter

I have a variety of situations where my dns settings need to be different. Its been a pain to manually change them every time so i decided to create a powershell script to automate this:

$wmi = (Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true' and DHCPEnabled='true'" | ? {$_.Description -eq "Intel Nic Adapter #2"}) $dnsServers = @("208.67.222.222", "208.67.220.220") $wmi.SetDNSServerSearchOrder($dnsServers)

The script executes without any errors but when i go check the specific adapter i’m targeting its settings haven’t changed from their default of obtaining the dns automatically. I don’t need to change any other setting except for the DNS Server addresses. I’ve checked the $wmi object above and i verified it is the adapter i want to be changed. I also tried to pass in a static string to SetDNSServerSearchOrder ($wmi.SetDNSServerSearchOrder(“208.67.220.220”)) but that didn’t work either. I’m not running the script as administrator and i’m assuming thats ok since i didn’t receive any errors when running the script. This probably doesn’t matter but this script is running on Windows 10 Fall Creators Update. Any help would be appreciated.

PoSH notwithstanding, on / in windows proper, system-wide settings and or device changes, even changing the system clock, requires admin privileges.

So, give this a shot in a PoSH admin session and see if the results are the same as non-admin.

Thanks postanote for the reply. That worked. Weird it didn’t give me any error Any idea why? Is it because i’m dealing with wmi and not a true powershell object?

There is a #requires you can add to the top of your script so that it will produce an error if you are not running as an admin. I have to use this every once in a while so that I remember months later.

#Requires -RunAsAdministrator

Read more here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_requires?view=powershell-5.1