How to block an IP or a Website using PowerShell in Windows 10

How to block an IP or a Website using PowerShell in Windows 10

PowerShell comes with a Netsecurity module that allows you to configure Windows firewall. You can use the function – New-NetFirewallRule – in Netsecurity to block an IP address or a website using PowerShell in Windows. The function allows you to create a new incoming or outgoing firewall rule and adds the rule to the target computer.

Block IP address or website using PowerShell

While blocking IP address ranges works perfectly, blocking a website or domain is tricky. This is because there can be multiple IP addresses attached to the domain, and although you can prevent them, the DNS resolver can determine a different IP address each time it queries. In addition, sometimes the same IP can be used by related services, and blocking that IP would also mean blocking other services.

  1. Block local or internet IP addresses
  2. Block websites or domain names

You will need administrator privileges to run them.

1]Block IP or range using PowerShell

Using this command, you can use a single IP address or a range of IP addresses. Run the following command in PowerShell.

New-NetFirewallRule -DisplayName "Block XYZ.com IP address" -Direction Outbound –LocalPort Any -Protocol TCP -Action Block -RemoteAddress 146.185.220.0/23

You can replace the XYZ.com Block IP address with anything you remember or make it easy to understand every time you think about it. The IP address mentioned at the end of the RemoteAddress option is what will be blocked. Any website or service that solves this problem will be blocked. You can replace the RemoteAddress option with the LocalAddress option if the IP is the IP address of the local network.

Block IP address in Windows firewall application

When the execution is complete, you should receive a status message like “The rule has been successfully scanned from the store.” (65536) ”. Open Windows Firewall and check if the entry is available. Once confirmed, you should be able to add more using PowerShell.

2]Block a website or domain using PowerShell

Block website domain using PowerShell

Since the function does not support URL blocking, we have two choices. The first is to query and block all possible IPs in this domain. The second is to find and block known official IP ranges. The latter is less likely to accidentally block other services compared to the former. That said, if blocking a domain is essential, you can still use other software to block it.

Resolve-DnsName "facebook.com"

Note the IP address that we will use in the second method

New-NetFirewallRule -DisplayName "Block XYZ.com IP address" -Direction Outbound –LocalPort Any -Protocol TCP -Action Block -RemoteAddress 146.185.220.0/23

When I used this with YouTube, it didn’t work even if the direct IP was blocked. When I used it with Facebook, it worked. So if a website can be resolved using multiple IP addresses, this method will not work.

Using PowerShell commands is simple. If you’ve ever used the command prompt, it’s as good as that; I hope you have successfully blocked IP or a website using PowerShell in Windows successfully. Whenever you want to remove them, you can do so from Windows Firewall or use the Remove-NetFirewallRule order.

  • Keywords: Firewall, PowerShell

Leave a Reply