Skip to content

Instantly share code, notes, and snippets.

@iamdroppy
Created June 17, 2023 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamdroppy/a6cc850994bc297eec0246118a79ba87 to your computer and use it in GitHub Desktop.
Save iamdroppy/a6cc850994bc297eec0246118a79ba87 to your computer and use it in GitHub Desktop.
# Check for admin privileges
$IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
if ($IsAdmin -eq $false) {
Write-Host "This script requires administrative privileges. Run the script as an administrator."
Exit
}
# Store all network interface in a variable
$adapters = Get-NetAdapter
# Display the network interfaces to the user in a grid view for selection
$selectedAdapter = $adapters | Out-GridView -Title 'Select your network interface' -PassThru
# Check to see if a network interface was selected
if($selectedAdapter) {
# Change DNS settings
Write-Host "Changing DNS settings..."
Set-DnsClientServerAddress -InterfaceIndex $selectedAdapter.ifIndex -Reset
Set-DnsClientServerAddress -InterfaceIndex $selectedAdapter.ifIndex -ServerAddresses ("8.8.8.8", "8.8.4.4")
# Disable IPv6 on the interface
Write-Host "Disabling IPv6..."
Set-NetAdapterBinding -Name $selectedAdapter.Name -ComponentID ms_tcpip6 -Enabled $false
# Run group policy update
Write-Host "Updating group policy..."
Invoke-GPUpdate /Force
# Disable automatic DNS settings in group policy
Write-Host "Disabling automatic DNS settings in group policy..."
Set-ItemProperty -Path "HKLM:\Software\Policies\Microsoft\Windows NT\DNSClient" -Name "DNSServers" -Value ""
Write-Host "Completed!"
} else {
Write-Host "No network interface selected. Exiting script."
}
@beegotsy
Copy link

Hello, I tried the script and it updates the IPv4 DNS settings, but the script fails on the next steps (on Windows 11 Home 22631.2715):

+     Invoke-GPUpdate /Force
+     ~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Invoke-GPUpdate:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

+     Set-ItemProperty -Path "HKLM:\Software\Policies\Microsoft\Windows ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (HKLM:\Software\...ws NT\DNSClient:String) [Set-ItemProperty], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetItemPropertyCommand

Do you think that this is important? I think the following line explains the cause of both errors; should I install RSAT Tools?

Invoke-GPUpdate is part of the GroupPolicy module. GroupPolicy is not available by default, but is included with RSAT Tools.

Thank you so much

@iamdroppy
Copy link
Author

Hello, I tried the script and it updates the IPv4 DNS settings, but the script fails on the next steps (on Windows 11 Home 22631.2715):

+     Invoke-GPUpdate /Force
+     ~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Invoke-GPUpdate:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

+     Set-ItemProperty -Path "HKLM:\Software\Policies\Microsoft\Windows ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (HKLM:\Software\...ws NT\DNSClient:String) [Set-ItemProperty], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetItemPropertyCommand

Do you think that this is important? I think the following line explains the cause of both errors; should I install RSAT Tools?

Invoke-GPUpdate is part of the GroupPolicy module. GroupPolicy is not available by default, but is included with RSAT Tools.

Thank you so much

interesting, haven't noticed that, can you confirm it works without Group Policy update? it shouldn't in some cases, on Pro I have to run it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment