The DNS cache in Windows systems is usually cleared with the command
ipconfig /flushdns
. This command requires administrative privileges, which creates a challenge in Windows Server environments where standard users need to refresh their DNS cache during troubleshooting or after network changes.
To work around this limitation, we'll create an automated system that consists of:
- One-time setup by the Administrator. A user with Administrator privileges creates and executes a PowerShell script, which runs continuously in the background with administrative privileges. Its sole purpose is to monitor a designated folder for any changes. When changes are detected, it executes the DNS flush command automatically.
- Standard User Operation. To clear the DNS cache, a standard user creates an empty file or folder in the monitored folder. The file or folder will disappear in about 10 seconds, indicating that the
ipconfig /flushdns
command has been executed.
This system maintains security by keeping administrative privileges within the PowerShell script, while standard users only perform file operations in a designated folder where they have been granted access rights.
1.Installation Steps Conducted by the Administrator
The following steps should be performed by a user with Administrator privileges. In this example, we'll use the C: drive, but you can adjust the folder path as needed.
1.1.Create the Monitored Folder
Create a folder named
flushdns that will be used for triggering the DNS cache clearing. Grant access to the folder for standard users, typically in the group "Users." Use the following PowerShell commands:
New-Item -Path "C:\flushdns" -ItemType Directory
icacls "C:\flushdns" /grant "Users:(OI)(CI)F"
The second command grants full access to all users. To restrict access to specific users or groups, replace "Users" with the appropriate user or group name.
1.2.Create the PowerShell Script
Create a PowerShell script file at
C:\scriptflushdns.ps1 with the following content:
# Path to the folder to monitor
$folderPath = "C:\flushdns"
###### DO NOT MODIFY BELOW THIS LINE
# Window title
$host.UI.RawUI.WindowTitle = "DNS Flush Process"
# Continuous loop
while ($true) {
$files = Get-ChildItem -Path $folderPath -File -ErrorAction SilentlyContinue
# If there are files
if ($files.Count -gt 0) {
# Execute ipconfig /flushdns
try {
$result = Start-Process "ipconfig.exe" -ArgumentList "/flushdns" -Verb RunAs -WindowStyle Hidden -Wait -PassThru
if ($result.ExitCode -ne 0) {
Write-Host "Error: DNS flush failed with code $($result.ExitCode)" -ForegroundColor Red
} else {
Write-Host "DNS cache successfully cleared at $(Get-Date)" -ForegroundColor Green
}
}
catch {
Write-Host "Error executing ipconfig: $($_.Exception.Message)" -ForegroundColor Red
}
# Delete all files
try {
Remove-Item -Path "$folderPath\*" -Force
}
catch {
Write-Host "Error deleting files: $($_.Exception.Message)" -ForegroundColor Red
}
}
# 10 seconds pause between checks
Start-Sleep -Seconds 10
}
1.3.Run the Script
As an Administrator, run
C:\scriptflushdns.ps1. This will open a PowerShell console window named "DNS Flush Process." Do not close this window, as the script runs continuously, monitoring the folder and executing the DNS cache clearing when triggered.
Whenever a DNS cache is cleared, the script prints the message:
DNS cache successfully cleared at ...
For automatic startup after system reboots, configure the script to run as a scheduled task.
2.Clear the DNS Cache as a Standard User
To clear the DNS cache as a standard Windows user, create an empty file or folder in
C:\flushdns.
After about 10 seconds, the created file or folder will disappear, indicating that the
ipconfig.exe /flushdns
command has been executed.
The content of this document is licensed by Interspace under the
MIT License