Windows OS Hub
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server 2025
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
    • Proxmox
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows 10
    • Windows Server 2025
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
    • Proxmox
  • PowerShell
  • Linux

 Windows OS Hub / Windows 10 / How to Block Unwanted Websites in Windows

September 16, 2025

How to Block Unwanted Websites in Windows

This post explains several methods for blocking access to specific websites, domains, URLs, or IP addresses on a Windows computer without using third-party tools. The most effective way to block websites is typically to configure restriction policies at the network gateway level (e.g., routers or Wi-Fi access points) or through third-party software (e.g., content filters, DNS filters, or parental controls). For this task, we will use only built-in Windows tools and PowerShell scripts to block unwanted websites.

Contents:
  • Block Websites Using the Hosts File in Windows
  • How to Block Websites or URLs via Group Policy
  • How to Block a Website IP Address in Windows Defender Firewall
  • Blocking Websites in Windows Firewall with PowerShell

Block Websites Using the Hosts File in Windows

The most well-known method for blocking a specific website on a Windows computer is to redirect traffic to that website to an invalid IP address by adding a fake entry to the hosts file. The hosts file is located in the %windir%\system32\drivers\etc\ directory (path can be changed via registry parameter DataBasePath under the registry key HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters).

hosts file in Windows

 The hosts file is used to manually assign mappings between IP addresses and DNS names. When performing name resolution, the hosts file takes precedence over the DNS servers specified in the network connection settings.

To edit the host file, open it in the elevated text editor. For example, run the PowerShell console as an administrator and execute the following command:

notepad $env:SystemRoot\System32\drivers\etc\hosts

To block a specific website (for example, facebook.com), add these entries to the hosts file:

127.0.0.1 facebook.com
127.0.0.1 www.facebook.com

using hosts file in windows to block domains and sites

Save the file and purge the client’s local DNS cache:

ipconfig /flushdns

After this, the ERR_CONNECTION_REFUSED/ Page not found / Page not available message will appear when trying to open the facebook.com website in any browser.

block website via hosts file

The following PowerShell functions can be used to automate adding or removing blocked websites from the hosts file.

Function BlockSiteHosts ( [Parameter(Mandatory=$true)]$Url) {
$hosts = "$env:SystemRoot\System32\drivers\etc\hosts"
$is_blocked = Get-Content -Path $hosts |
Select-String -Pattern ([regex]::Escape($Url))
If(-not $is_blocked) {
$hoststr="127.0.0.1 ” + $Url
Add-Content -Path $hosts -Value $hoststr
}
}

Function UnBlockSiteHosts ( [Parameter(Mandatory=$true)]$Url) {
$hosts = "$env:SystemRoot\System32\drivers\etc\hosts"
$is_blocked = Get-Content -Path $hosts |
Select-String -Pattern ([regex]::Escape($Url))
If($is_blocked) {
$newhosts = Get-Content -Path $hosts |
Where-Object {
$_ -notmatch ([regex]::Escape($Url))
}
Set-Content -Path $hosts -Value $newhosts
}
}

block websites in hosts file using powershell

Add these functions to your PowerShell profile file (Microsoft.PowerShell_profile.ps1). Then, to add a website to the blocked ones, run the following command:

BlockSiteHosts ("facebook.com")

To unblock the website:

UnBlockSiteHosts ("facebook.com")

 However, if you have configured DNS over HTTPS name resolution, your browsers may ignore entries in the hosts file.  Mozilla Firefox, in particular, uses DoH resolution via Cloudflare servers by default. Therefore, this method of blocking websites will not be 100% effective. dns over https enabled in firefox

How to Block Websites or URLs via Group Policy

Group Policies (GPOs) can be used to block users from accessing specific websites on their browsers. The only caveat is that each browser has its own set of Group Policy templates, which are not compatible with other browsers. Therefore, the administrator must first prevent the launch of any browsers except those permitted by AppLocker, SRP, or other software restriction policies.

Now, let’s look at how to block a website in the Microsoft Edge browser:

  1. Download and install the administrative GPO templates for the Microsoft Edge browser. (learn more about installing ADMX templates in Windows)
  2. Open the local Group Policy Editor (gpedit.msc) or the Group Policy Management Console (gpmc.msc) if you want to block access to websites on multiple machines in the domain.
  3. Go to Computer Configuration -> Policies -> Administrative Templates -> Microsoft Edge.
  4. Select the Block access to a list of URLs option, click “Show“, and add a list of website addresses to block.GPO: block website urls in browser
  5. Save the policy settings and then update them on the client: gpupdate /force

If a user tries to open a blocked website using the Microsoft Edge browser, the following message will appear:

Website is blocked
Your organization doesn’t allow you to view this site.

website blocked by organization policies

If Google Chrome is your primary browser, you need to download and install the ADMX policies for Chrome. Then, configure the Block access to a list of URLs parameter.

Google Crome: Block access to a list of URLs via group policy

GPOs for browsers allow you to create flexible site access restrictions and/or exceptions, depending on whether you want to implement a white or blacklist of sites.

How to Block a Website IP Address in Windows Defender Firewall

You can use the built-in Windows Firewall rules to block specific websites or domains. The main disadvantage of this method is that you won’t be able to use the name of a domain or website URL in the blocking rule. Windows Firewall only allows you to specify an IP address or subnet as the source or destination.

First, you have to find the IP address of the website you want to block. Resolve the sitename to IP using the command:

nslookup x.com

or

Resolve-DnsName x.com

nslookup get ip address by domain name

Several IP addresses were returned for the domain. You have to block them all.

Run the Windows Defender Firewall management console (firewall.cpl ).

In the Outbound Rules section, create a new rule with the following settings:

create new outbound rule on windows firewall

  • Rule Type: Custom
  • Program: All programs
  • Protocol Type: Any
  • Scope: In the “Which remote IP addresses does this rule apply to?” section, select “These IP addresses” -> Add. In the next window, add the IP addresses, subnets, or a range of IP addresses you want to block.

outbound rule add IPs

Click OK -> Next -> Action -> Block the connection.

windows firewall block connection

Leave all options selected in the list of firewall profiles (network types) to which this rule applies. Then specify the rule name and save it.

A single Windows Defender Firewall rule is limited in the number of IP addresses and subnets it can contain. It is not recommended to specify more than 3,000 address objects in one firewall rule.

In an Active Directory (AD) domain, you can use a Group Policy Object (GPO) to deploy a firewall rule that blocks websites on users’ computers. However, this is not rational.  It is better to filter websites on your Internet access router (gateway).

Blocking Websites in Windows Firewall with PowerShell

You can use PowerShell to create a firewall rule that blocks outgoing connections to a particular website.

New-NetFirewallRule -DisplayName "Block Site" -Direction Outbound –LocalPort Any -Protocol Any -Action Block -RemoteAddress 104.244.42.129, 104.244.42.0/24

New-NetFirewallRule: create block rule

The Resolve-DnsName PowerShell cmdlet can automatically resolve a website name to an IP address.

Resolve-DnsName "x.com"| Select-Object -ExpandProperty IPAddress

Use the following script to resolve the site name to an IP address and immediately add a rule blocking access to it.

$SitesToBlock = "facebook.com","instagram.com","youtube.com"
$IPAddress = $SitesToBlock | Resolve-DnsName -NoHostsFile | Select-Object -ExpandProperty IPAddress
New-NetFirewallRule -DisplayName "Block Web Sites" -Direction Outbound –LocalPort Any -Protocol Any -Action Block -RemoteAddress $IPAddress

The -NoHostsFile parameter is used to ignore the hosts file for resolving.

Make sure that an outbound block rule has appeared in the Windows Firewall console.

windows firewall rule to block websites by domain name

This article is mostly a brain-training exercise. To restrict access to websites within a corporate network, filtering must be implemented at the internet access gateway or proxy server level. It is not as effective to block website access at the host level.

3 comments
6
Facebook Twitter Google + Pinterest
Group PoliciesPowerShellWindows 10Windows 11
previous post
Fixing Volume Shadow Copy (VSS) Error with Event ID 8193
next post
How to Change Computer Object Attributes in Active Directory

Related Reading

Fixing “Winload.efi is Missing or Contains Errors” in...

March 16, 2024

Protecting Remote Desktop (RDP) Host from Brute Force...

February 5, 2024

Fix: Photos App in Windows 10 Opens Extremely...

April 19, 2023

How to Disable UAC Prompt for Specific Applications...

March 11, 2024

Managing Administrative Shares (Admin$, IPC$, C$) on Windows

March 15, 2024

How to Refresh (Update) Group Policy Settings on...

August 13, 2024

How to Install Only Specific Apps in Office...

March 12, 2024

How to Upgrade Windows Build from an ISO...

November 7, 2024

3 comments

TomB April 12, 2020 - 5:08 pm

Two points:

1. Resolving an URL to an IP and then blocking that IP is hardly a guarantee of prevention. In some cases, there are many IPs that lead to URL, in some cases an IP leads to many URLs which may be ones you need or want to get access to. You should mention this aspect when you talk about that aspect of things.

2. I just ran into the oddest scenario which has me now pointing undesirable URLs to an IP address in my local net that my DHCP server will never assign (nor will I) vs. the loopback interface (127.0.0.1). Why?

Well, I found with some of the google stuff I was trying to block and routed to 127.0.0.1, when I did a netstat -s or netstat -a -n -o, I observed that there were a bunch of connections targeted at the name the google software was looking for that had *CONNECTED* to something on 127.0.0.1 (both the local and foreign addresses were on the loopback interface). I bet there are apps that you want to block that act both as client and server so looping back forms a connection back to your own machine. While this does keep those attempts back within your machine, it still seems undesirable.

My solution here was to identify a network address locally that DNE and never will (outside my DHCP allocation pool, will not be statically created by me – which also implies that I have entire control over the network which I do) and then use the hosts file to send these requests to a dead end. Is that better than having them loopback, never hit the network, and connect to something locally? I really don’t know. I am hoping with the address translation from an URL to a host in the hosts file (to a dead address on the network) and then using Windows Defender to block outgoing requests for that dead IP, that combination will result in the firewall just quietly eating the outside connect requests so a) there will be no connections back to my machine in loopback and b) there will be no extra connection requests firing into the local network (because Defender blocked them).

Just some extra strategies for the thought experiment. I do appreciate the article for the knowledge you have conveyed – thank you.

Reply
TomB April 12, 2020 - 5:17 pm

One further point: If you don’t control the network, there are still ‘reserved’ internet address ranges that for the most part are not supposed to be used in normal networks. It is possible for you to use one of those ranges to send the requests to (as it isn’t likely to catch a host on your network) and then kill that address with IP based blocking in Defender Firewall.

Note, do not use the Link Address range (which is used for when a DHCP server is not available and DHCP clients are trying to assign themselves (no central direction) an IP address that does not conflict with another using trial-error-retry with new address approach). Leave that address band alone, but there are still other reserved address ranges you could use. I’d still put a Firewall block on outgoing traffic to those just to not put things onto your local net and make your router/switch/access point/etc have to make a decision about what to do with that traffic (why waste the devices time?). Block it locally as an outgoing address on your box.

In MOST cases, this approach should not impede any local software.

One somewhat useful (depends on how machines on your network setup ping handling) way to test a reserved address on your local net would be to ping it from the command line. That would tell you if it was occupied if you got a ping response. (Does not tell you nothing might be there if you do not – machine could be off temporarily or be set not to return ICMP echoes when pinged). But if you do get a response on a reserved internet address, try another. There are 3 or 4 ranges with a lot of addresses in them in the list of reserved addresses.

(PS I’m talking IPv4 here – haven’t looked into any reserved address ranges in IP V6)

Reply
John O July 31, 2023 - 5:51 am

Not sure how out of date this is, but the directory structure in REGEDIT is not the same. I am running windows 10.

I would suggest specifying the windows OS version in this, or just deleting it. Neither will happen probably. Just another Orphaned webpage on the vast superhighway of information.

Reply

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Recent Posts

  • Windows Stucks at ‘Getting Windows Ready, Don’t Turn Off Computer ‘

    September 15, 2025
  • Clean Up ETL Log Files in ProgramData

    September 9, 2025
  • Fix: Slow Startup of PowerShell Console and Scripts

    September 3, 2025
  • DPI Scaling and Font Size in RDP (RDS) Session

    August 27, 2025
  • Proxmox: Share a Host Directory with VMs via VirtioFS

    August 18, 2025
  • How to Find AD Users with Blank Passwords (Password-Not-Required)

    July 24, 2025
  • Run Elevated Commands with Sudo on Windows 11

    July 16, 2025
  • Find a Process Causing High Disk Usage on Windows

    July 15, 2025
  • Fix: Microsoft Defender Not Updating Automatically in Windows

    July 8, 2025
  • Create a Windows Server VM on Proxmox (Step-by-Step)

    July 7, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • How to Hide or Show User Accounts from Login Screen on Windows
  • Configuring Proxy Settings on Windows Using Group Policy Preferences
  • Using WMI Filters to Target Group Policies in Active Directory
  • How to Disable NetBIOS, LLMNR, mDNS Protocols in Windows
  • Set Desktop Wallpaper and Logon Screen Background via Group Policy
  • How to Disable/Remove Thumbs.db File on Network Folders in Windows
  • How to Disable or Change User Account Control (UAC) Settings in Windows
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top