PowerShell Set DNS Settings on All Systems

#======================
#Get List of Computers
#======================

$MyComputers=Get-ADComputer -filter * -Properties * | Where{($_.Enabled -eq $True) -and ($_.Operatingsystem -like "Windows*")} | Select -ExpandProperty Name 

#====================
#Instantiate Array
#===================

$MyArray=@()
$MyFails=@()

#===============================
#Check Access to Each Computer 
#===============================

Foreach($Comp in $MyComputers){
        
    If((Invoke-Command -ErrorAction SilentlyContinue –ComputerName $comp –ScriptBlock {1}) –eq 1){
        $MyArray+=$Comp
    }Else{
        $MyFails+=$Comp
    }
}

#====================================================
# Export Pass failed for Further Review 
#====================================================

$MyArray | Out-File C:\temp\PassedDNSSystems.txt
$MyFails | Out-File C:\temp\FailedDNSSystems.txt


$MyCommand={   
   
# Update Double Quoted and Commma Separated DNS Servers by IP
    $newDNSServers = "127.0.0.1","172.31.47.11","192.168.0.1"

    # Get all network adapters that already have DNS servers set
    $adapters = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DNSServerSearchOrder -ne $null}

    # Set the DNS server search order for all of the previously-found adapters
    $adapters | ForEach-Object {$_.SetDNSServerSearchOrder($newDNSServers)}

    #Get New DNS Settings and Return in Object
    $MyNS=Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {($_.DNSServerSearchOrder -ne $null)} | Select -ExpandProperty DNSServerSearchOrder

   
    #===================================
    # Pull DNS Settings add to Object
    #===================================

    $Obj=New-Object PSObject 
    $Obj |Add-Member NoteProperty ServerName ($env:COMPUTERNAME)
    $Obj |Add-Member NoteProperty DNS_Settings ($MyNS)
    Return $Obj    

}


#===============================================
# ------ Execution & Export to File ------------
#===============================================

$MyReturnValues=Invoke-Command $MyArray -ScriptBlock $MyCommand

$MyReturnValues | Select ServerName, DNS_Settings | Where{$_.ServerName -ne $NULL} | Export-Csv C:\temp\NewDNS.csv -NoTypeInformation -Append

Used Some Code From: 

https://daniel.streefkerkonline.com/2014/12/09/change-a-nics-dns-server-settings-with-powershell-and-wmi-setdnsserversearchorder/