Setting a static DNS address on an adapter

Welcome Forums General PowerShell Q&A Setting a static DNS address on an adapter

This topic contains 3 replies, has 3 voices, and was last updated by

11 months, 2 weeks ago.

  • Author
  • #90541

    Points: 0
    Rank: Member

    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 = @("", "")

    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("")) 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.

  • #90544

    Points: 320
    Helping Hand
    Rank: Contributor

    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.

    • #90548

      Points: 0
      Rank: Member

      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?

  • #90700

    Points: 31
    Rank: Member

    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:

The topic ‘Setting a static DNS address on an adapter’ is closed to new replies.