Themes / Apps / Mods [Windows][PowerShell][Script]psAdBlock - PowerShell AdBlock Script for Windows

Search This thread

Makorus

Senior Member
Oct 4, 2012
195
171
github.com
Introduction
Dear community

This is my very first XDA: DevDB project. I'm glad to announce my ad-block solution purely based on PowerShell.
I've been (and I still am) using AdAway on my Android phone. It's a great app, and it inspired me to get a similar solution for Windows computers.

Version 0.1 was not public because it was slow as hell. It took about 8 minutes to handle ~200'000 hosts.
With version 1.0, my script got a massive speed improvement. Now its duration has been greatly decreased. I achieved the same amount of hosts in less than 100 seconds now. But of course, these measured values shouldn't mean a lot, as they depend completely on your hardware (and a little bit on your internet connection of course).

Now version 1.6 has an automatic backup & apply routine, check it out!

Features
This script does exactly what you think: It grabs various "hosts" sources from the internet, saves them locally, and merges them to one big file. Enjoy an ad-free Windows experience.

  • Download as many "hosts" files as you want
  • Merge them automatically into one big "hosts" file
  • Remove duplicates from the "hosts" file
  • Remove any comment line starting with # to keep your hosts file as small as possible
  • Apply a custom whitelist or even custom blacklists
  • Log the entire process to a log file (placed within the same directory where you saved this script)

Downloads & Changelogs

FAQs

Disclaimer
Altough this is normally a safe process, I am not responsible for possible damage to your device if anything should fail.
I will not take any responsability for bricked Windows installations, other software defects or even lost data.

License/forks
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License( ).

This is an open source project, it's a PowerShell script and you're able to see the source code of course. You're allowed to modify this script as you want, as long as you give credits to me. Adaptations of my work shall be shared as well under the same license. Commercial use is allowed, as long as you don't sell my work.

Requirements
There are some requirements to check.
  • A little bit of PowerShell-knowledge (not necessary of course)
  • At least Windows 7 (theoretically it should work on any Windows)
  • At least PowerShell 3.0 (PowerShell 2.0 may work, but is unsupported)
  • At least 50 MB free space for this script
  • Administrative privileges - required if you want to use the automatic backup & apply routines, not for the script itself, otherwise you'll need to manually copy the "hosts" file into the Windows directory

Instructions for use with administrator privileges (recommended)
  1. Download the archive and unzip it anywhere
  2. Execute the "_Run_Admin.bat" script via double-click, confirm the UAC window by clicking on the yes button and wait for the console to close itself
  3. Reboot your machine and enjoy an ad-free Windows experience
  4. Note: Instead of the batch script you can use the .ps1 file as well (unblock it first, simply right-click the .ps1 file, choose "Properties", then hit the "Unblock" button), start a new PowerShell console as administrator and manually run the script

Instructions for use without administrator privileges
  1. Download the archive and unzip it anywhere
  2. Execute the "_Run_User.bat" script via double-click and wait for the console to close itself
  3. Copy the newly created "hosts" file and rename the existing "hosts" file to manually create a backup
    Code:
    C:/Windows/System32/drivers/etc/hosts
  4. Reboot your machine and enjoy an ad-free Windows experience
  5. Note: Instead of the batch script you can use the .ps1 file as well (unblock it first, simply right-click the .ps1 file, choose "Properties", then hit the "Unblock" button), start a new PowerShell console and manually run the script

Instructions to customize psAdBlock
  1. To modify the "hosts" sources, simply edit the file "hostslist.ini"
  2. To allow specific hosts, simply edit the file "whitelist.ini"
  3. To block specific hosts, simply create a new .txt file within the "DL_Cache" folder and add the hosts (one per line) you want to block (e.g. "0.0.0.0 place-any-site-here.com")

Credits & thanks to
* Makorus: Creator of this PowerShell script
* Dominik Schuermann: Inspiring me to do this
* Luca Sturlese: Allowing us to use his awesome logging function library

Any questions or suggestions? Did you find any bugs? Thanks for your feedbacks in advance! :good:

XDA:DevDB Information
psAdBlock, Tool/Utility for the Windows 8 General

Contributors
Makorus
Source Code: https://github.com/makorus/ps-adblock


Version Information
Status: Stable
Current Stable Version: 1.6.5
Stable Release Date: 2017-07-03

Created 2015-05-09
Last Updated 2018-07-13
 
Last edited:

Makorus

Senior Member
Oct 4, 2012
195
171
github.com
News
I'm moving this project to GitHub for many reasons. Future releases will be available through GitHub only.

New download link:
https://github.com/makorus/PS-AdBlock/archive/master.zip


Current release & changelog
Code:
##-------------------------------------------------------------------------------------##
## V1.6.3, 2018-02-03                                                                  ##
## - Added logic to handle 'localhost' in whitelist (thanks to antonio-gil)            ##
## - Removed script version from filename                                              ##
##-------------------------------------------------------------------------------------##

Notes
Code:
#########################################################################################
## Notes:                                                                              ##
## - PowerShell 3.0 or newer is recommended to run this script without any errors      ##
## - PowerShell 2.0 may work as well, but is unsupported                               ##
## - If you disable the backup/apply mode you have to manually copy the final          ##
##   'hosts'-file to the Windows etc directory (please manually backup the old file:   ##
##   C:\Windows\System32\drivers\etc                                                   ##
## - To apply a custom blacklist, simply create a text file containing your desired    ##
##   hosts and save it in the download cache folder 'DL_Cache' (run the script once)   ##
## - To manually add your custom hosts list, create a new text file called             ##
##   'hostslist.ini' and add your hosts sources (one site per line without the www)    ##
## - To automatically remove specific sites from the hosts list, create a new text     ##
##   file called 'whitelist.ini" and add your sites you want to whitelist              ##
## - If you experience performance issues like extremely slow Windows startup time,    ##
##   Try to disable the client DNS Cache service via CMD:                              ##
##     sc config dnscache start= disabled                                              ##
#########################################################################################

Release history & changelogs
Code:
#########################################################################################
## Release history (yyyy-MM-dd):                                                       ##
##-------------------------------------------------------------------------------------##
## V0.0.1 - V0.0.9, 2015-05-02                                                         ##
## - Non-public test versions only                                                     ##
##-------------------------------------------------------------------------------------##
## V1.0.0, 2015-05-09                                                                  ##
## - First public and stable release                                                   ##
##-------------------------------------------------------------------------------------##
## V1.1.0, 2015-05-10                                                                  ##
## - Added release history to the script                                               ##
## - Merged Log Functions to the main script file (no more two separate files)         ##
## - Added XDA:DevDB link                                                              ##
## - Added script version to name (easy identification)                                ##
##-------------------------------------------------------------------------------------##
## V1.2.0, 2015-05-13                                                                  ##
## - Fixed issue with old 'New-Item' command in function 'Log-Start':                  ##
## --> OLD: New-Item -Path $LogPath -Value $LogName -ItemType File                     ##
## --> NEW: New-Item -Path "$sFullPath" -ItemType File                                 ##
## - Changed the 'Log-Start' invoking command to use $PSScriptRoot:                    ##
## --> OLD: Log-Start -LogPath ".\" [...]                                              ##
## --> NEW: Log-Start -LogPath "$PSScriptRoot" [...]                                   ##
## - Added #region and #endregion codes to fold the code                               ##
##-------------------------------------------------------------------------------------##
## V1.3.0, 2015-05-13                                                                  ##
## - Completely reworked the 'release history', huge improvement to readability        ##
## - The 'Patch Table' uses now '-ireplace' instead '-replace' for better results      ##
## - Changed the 'Patch Table' to use regex expressions for my default list            ##
## - Added Spotify to 'Patch Table':                                                   ##
## --> $DefaultPatchTable.Add('\w+.spotify.com', 'localhost')                          ##
##-------------------------------------------------------------------------------------##
## V1.4.0, 2015-07-26                                                                  ##
## - Reworked whitelist method (check notes for more information)                      ##
## - Reworked hosts source list (check notes for more information)                     ##
## - Reworked the 'Patch Table', doesn't require regex expressions anymore             ##
##-------------------------------------------------------------------------------------##
## V1.5.0, 2015-08-10                                                                  ##
## - The file now checks for the client DNS Cache service and logs a warning if it's   ##
##   enabled to ensure you don't run into troubles                                     ##
## - Removed the note about the DNS Cache service: It's not recommended to set the     ##
##   startup type to "demand" (manual), it will cause issues sooner or later           ##
## - Starting from now on, I'll create an executable file (to be found in the ZIP)     ##
## - I'll also attach a VirusTotal.com scan to every new release                       ##
## --> Please report any Anti-Virus false positive alarms to me                        ##
##-------------------------------------------------------------------------------------##
## V1.5.1, 2015-08-11                                                                  ##
## - Fixed issue with wrong $PSScriptRoot variable when using the EXE variant          ##
## - Changed the 'release history' to allow better versioning (e.g. version 1.5.1)     ##
## --> Note: Older versions still contain two numbers only (e.g. version 1.2 etc.)     ##
##-------------------------------------------------------------------------------------##
## V1.5.2, 2015-09-13                                                                  ##
## - Remove every empty line in the hosts file                                         ##
## - Changed the Log-Write function to use Write-Host instead of Write-Debug, now      ##
##   the entire log will be shown during the script execution (live)                   ##
## - Changed replacement method to insert 0.0.0.0 instead of 127.0.0.1 in the final    ##
##   hosts file which leads to a much better and faster DNS resolution                 ##
## - Future releases will be delivered with additional hosts files, for example to     ##
##   block Google ad servers and some Windows 10 hosts to stop this privacy nightmare  ##
##-------------------------------------------------------------------------------------##
## V1.5.3, 2015-10-08                                                                  ##
## - Fixed ugly encoding issue, new 'hosts' file will be created correctly now, means  ##
##   no BOM (Byte Order Mark) and no UCS-2 encoding anymore, should fix many issues    ##
##-------------------------------------------------------------------------------------##
## V1.5.4, 2015-10-09                                                                  ##
## - Fixed duplicate hostname issue, cmdlet Get-Unique allows by default hostnames     ##
##   with capital letters (Get-Unique is a case sensitive search)                      ##
##   e.g. 'test.com' and 'Test.com' are being handled correctly now                    ##
## - Reverted warning about DNS cache service, there should be no issue with the DNS   ##
##   cache service because the 'hosts' file is being encoded as a typical UTF8 file    ##
## - Added new debug mode, set $DebugMode to $true to activate it                      ##
##   This skips the download process, useful if you want to test something without     ##
##   re-downloading the whole host source lists                                        ##
##-------------------------------------------------------------------------------------##
## V1.5.5, 2015-10-10                                                                  ##
## - Fixed issue with method ToLower (should work with PowerShell 2.0 now), ToLower    ##
##   is a method to work with strings only, work around with a ForEach-Object cmdlet   ##
## - Added a PowerShell version check, unsupported versions are PowerShell v1 and v2,  ##
##   if you start this script with an unsupported PowerShell version, you'll find      ##
##   some hints in the log file to inform you about that                               ##
##-------------------------------------------------------------------------------------##
## V1.5.6, 2015-10-11                                                                  ##
## - Added again regex patterns to patch table, whitelist should work now correctly    ##
## - Debug mode now skips the patch process as well                                    ##
##-------------------------------------------------------------------------------------##
## V1.5.7, 2015-10-17                                                                  ##
## - Completely replaced previous regex patterns with one more complex pattern to      ##
##   handle hostnames with subdomains, performance increased significantly             ##
##-------------------------------------------------------------------------------------##
## V1.5.8, 2016-01-03                                                                  ##
## - Non-public test version only (skipped)                                            ##
##-------------------------------------------------------------------------------------##
## V1.5.9, 2016-01-24                                                                  ##
## - Non-public test version only (skipped)                                            ##
##-------------------------------------------------------------------------------------##
## V1.6.0, 2016-02-29                                                                  ##
## - Added function to check for admin privileges                                      ##
## - Added routine for automatic apply of 'hosts'-file (admin privileges required)     ##
## - Added routine for backup of 'hosts'-file (admin privileges required)              ##
## - Added some Skype and YouTube hostnames to the whitelist for proper functionality  ##
## - Added new icon for the executable file                                            ##
## - Updated the notes (see below)                                                     ##
## - Overall cleanup, added region codes for the main script                           ##
##-------------------------------------------------------------------------------------##
## V1.6.1, 2016-04-19                                                                  ##
## - Fixed wrong script number                                                         ##
##-------------------------------------------------------------------------------------##
## V1.6.2, 2017-04-25                                                                  ##
## - Added Goo.gl to 'Patch Table' and to the default whitelist.ini:                   ##
## --> $DefaultPatchTable.Add('\w+.goo.gl', 'localhost')                               ##
## - Changed the foreach loop to avoid recreating the object System.Net.WebClient      ##
## - Added error handling for the download process                                     ##
## - Added routine to correctly check the existence of the original 'hosts'-file       ##
## - Removed EXE file because of false positive alarms from many AntiVirus engines     ##
## - Added instead two new batch files: '_Run_Admin.bat' and '_Run_User.bat'           ##
## - Overall rework, changes many comments and updated some notes                      ##
##-------------------------------------------------------------------------------------##
 

Attachments

  • PS_AdBlock_V1.1.zip
    4.3 KB · Views: 150
  • PS_AdBlock_V1.0.zip
    4.7 KB · Views: 644
  • PS_AdBlock_V1.2.zip
    4.4 KB · Views: 68
  • PS_AdBlock_V1.3.zip
    4.7 KB · Views: 384
  • PS_AdBlock_V1.4.zip
    5.5 KB · Views: 316
  • PS_AdBlock_V1.5.zip
    227.4 KB · Views: 89
  • PS_AdBlock_V1.5.1.zip
    227.7 KB · Views: 374
  • PS_AdBlock_V1.5.2.zip
    230.8 KB · Views: 292
  • PS_AdBlock_V1.5.3.zip
    231.3 KB · Views: 87
  • PS_AdBlock_V1.5.4.zip
    232.3 KB · Views: 75
  • PS_AdBlock_V1.5.5.zip
    233 KB · Views: 57
  • PS_AdBlock_V1.5.6.zip
    233.5 KB · Views: 137
  • PS_AdBlock_V1.5.7.zip
    233.4 KB · Views: 1,639
  • PS_AdBlock_V1.6.0.zip
    192 KB · Views: 738
  • PS_AdBlock_V1.6.1.zip
    192.1 KB · Views: 4,435
  • PS_AdBlock_V1.6.2.zip
    14.3 KB · Views: 2,178
  • PS_AdBlock_V1.6.3.zip
    15 KB · Views: 2,725
Last edited:

Makorus

Senior Member
Oct 4, 2012
195
171
github.com
FAQs
  • How to execute this PowerShell script?
    Option 1: Run the script using one of the included batch scripts, either the "_Run_Admin.bat" or the "_Run_User.bat" batch script.
    Option 2: Manually run the PowerShell script. But at first, you have to unblock the PowerShell script because it is a script you downloaded from the internet. Once you unzipped the file, do a right click on the PS1 file and open its file properties. Click on the "Unblock" button and you're done. Right click the file and click on "Run with PowerShell".
  • Why do I get an message warning me about an execution policy?
    Microsoft implemented this security feature when they published PowerShell. By default, the execution policy is set to "restricted", which means no scripts can be executed directly. You're able to configure the execution policy as you want. There are several execution policies, you can check them out here: Microsoft TechNet, Set-ExecutionPolicy.
    I personally recommend to change the execution policy to "unrestricted", this means you can execute scripts you created and even those you downloaded from the internet (though you have to be careful what you download and what you execute).
  • How to uninstall this PowerShell script?
    Basically, you just have to navigate to this folder:
    C:\Windows\System32\drivers\etc
    Delete the new hosts file and restore the original one by renaming "hosts.original" to "hosts" without any extension. After that, simply delete the files you downloaded from this thread.
 
Last edited:

Gionathan

Member
Jul 14, 2013
48
8
Awesome! Can't wait to try this! I've used HostsMan, which is the closest UI-enabled program similar to AdAway for Windows, but it's really tricky to get it to work and it used to break my internet a lot. Had to go in safe mode to restore the original hosts file.
 

Makorus

Senior Member
Oct 4, 2012
195
171
github.com
Glad to read that, I hope you like it! Never heard of it, but I just took a look at it. I guess this tool doesn't disable the DNS cache service.
I'm thinking of implementing a check, to verify whether the DNS cache service is enabled or disabled.
 
  • Like
Reactions: vagsvag

Makorus

Senior Member
Oct 4, 2012
195
171
github.com
Hello everyone

I just uploaded two new versions, V1.2 and V1.3. I also made many other improvements, read the full change log for more information. :)
 
  • Like
Reactions: Alex9090

Makorus

Senior Member
Oct 4, 2012
195
171
github.com
Hi everyone!

Sorry for the delay, but I had many things to sort out in my real life. However, I was able to work on V1.4 for you guys.
Now you can work with two text files called "whitelist.ini" and "hostslist.ini" to manage this script - there's no need to edit the PowerShell script file itself. Also, there are many improvements, read the full change log for more information.
 
  • Like
Reactions: mrTGF

mrTGF

Member
Mar 23, 2015
40
19
thanks for this, it is is exactly what i was looking for, right now im trying to have this run on windows 10, i have a question the created "hosts" file is detected as a trojan virus, im wondering is this a false positive. i have backed up my system and original hosts file just in case. Hope there will be more developers for the Windsows platform so that we can make it a better OS, Ill let you know how it worked out, THNX

Just tried on Windsows 10, definitely not a virus, but i did experience allot lag with it, it blocks some ads in Microsoft Edge, runs
 
Last edited:

godusopp

Member
Aug 9, 2015
6
0
Thanks for this great tool. Works great but can someone make an .exe out of it? It would be more convenient.
 

godusopp

Member
Aug 9, 2015
6
0
Hi there, thank you for your reply. I can compile an EXE file out of it, but I expect a lot of false reports from anti-virus applications. I'll upload the EXE file tomorrow.

Kind regards
Makorus

Thanks for the great work. I really appreciate it. I'll whitelist if antivirus detect it as false positive :)
 

Makorus

Senior Member
Oct 4, 2012
195
171
github.com
Hi everyone

I just uploaded version 1.5. Go check it out! :laugh::good:
@Dj_Art Hey there

Hmm, it looks very strange. I guess you've got a Russian Windows 8.1 OS, am I right? Could you try out version 1.5 please?
Can you try placing the PS1 script on your desktop or somewhere else, where you have full access rights? Might be a permission issue as well.

Otherwise please hand out your system information (start -> run -> msinfo32 -> save as NFO file) so I can start debugging your error message.
@godusopp I've uploaded the EXE file as well, check the new ZIP file! :good:
@mrTGF Sorry I missed your post somehow. What Anti-Virus are you using?
The script itself is 100% clean, you can check out the source if you want. But I can't guarantee that the hosts sources I'm using are not compromised or whatever (but to be honest, I don't think so). ;)
 

mrTGF

Member
Mar 23, 2015
40
19
I have just tried the new update and it doesnt work for me at all, Microsoft Edge slows down drastically, the only difference in terms of ads, is that they end up loading last, in regards to the false positive i have used AVG Antivirus 2014, the file definitely is not a Trojan. I was wondeing if you can use the same host sources that Adaway on android is using?



Hi everyone

I just uploaded version 1.5. Go check it out! :laugh::good:

@Dj_Art Hey there

Hmm, it looks very strange. I guess you've got a Russian Windows 8.1 OS, am I right? Could you try out version 1.5 please?
Can you try placing the PS1 script on your desktop or somewhere else, where you have full access rights? Might be a permission issue as well.

Otherwise please hand out your system information (start -> run -> msinfo32 -> save as NFO file) so I can start debugging your error message.

@godusopp I've uploaded the EXE file as well, check the new ZIP file! :good:

@mrTGF Sorry I missed your post somehow. What Anti-Virus are you using?
The script itself is 100% clean, you can check out the source if you want. But I can't guarantee that the hosts sources I'm using are not compromised or whatever (but to be honest, I don't think so). ;)

Hi there, thank you for your reply. I can compile an EXE file out of it, but I expect a lot of false reports from anti-virus applications. I'll upload the EXE file tomorrow.

Kind regards
Makorus
 

Makorus

Senior Member
Oct 4, 2012
195
171
github.com
I have just tried the new update and it doesnt work for me at all, Microsoft Edge slows down drastically, the only difference in terms of ads, is that they end up loading last, in regards to the false positive i have used AVG Antivirus 2014, the file definitely is not a Trojan. I was wondeing if you can use the same host sources that Adaway on android is using?

Hi again

That's really strange, but this might be an issue with Windows 10/Microsoft Edge. I haven't tried yet how the hosts file reacts on a Windows 10 device. But I've got a Windows 10 device at home, so I'm gonna try it out as soon as possible and report back.
In the mean time, would you be so kind and post the version 1.5 log file? I think your DNS service might be online and that's why you face these issues.

Also, thanks for the information to your false positive alarm. Maybe you've already seen the VirusTotal.com link in my second post. You'll find TONS of false reports there. :(

Oh and by the way, actually, I'm using exactly the same hosts sources as the AdAway app on Android does.
 

godusopp

Member
Aug 9, 2015
6
0
Hi again

That's really strange, but this might be an issue with Windows 10/Microsoft Edge. I haven't tried yet how the hosts file reacts on a Windows 10 device. But I've got a Windows 10 device at home, so I'm gonna try it out as soon as possible and report back.
In the mean time, would you be so kind and post the version 1.5 log file? I think your DNS service might be online and that's why you face these issues.

Also, thanks for the information to your false positive alarm. Maybe you've already seen the VirusTotal.com link in my second post. You'll find TONS of false reports there. :(

Oh and by the way, actually, I'm using exactly the same hosts sources as the AdAway app on Android does.

Thanks for the app. I've already reported it as false positive to bitdefender so hopefully they solve it soon.
 

mrTGF

Member
Mar 23, 2015
40
19
Hi again

That's really strange, but this might be an issue with Windows 10/Microsoft Edge. I haven't tried yet how the hosts file reacts on a Windows 10 device. But I've got a Windows 10 device at home, so I'm gonna try it out as soon as possible and report back.
In the mean time, would you be so kind and post the version 1.5 log file? I think your DNS service might be online and that's why you face these issues.

Also, thanks for the information to your false positive alarm. Maybe you've already seen the VirusTotal.com link in my second post. You'll find TONS of false reports there. :(

Oh and by the way, actually, I'm using exactly the same hosts sources as the AdAway app on Android does.

Hi here is the log file you have asked for, i hope this helps. Also a little update, i went ahead and converted the hosts file to .txt, and had removed the very first empty line, after i put it back, this removed the lag from Microsoft Edge, however the ads still popped up after everything else loaded.
 

Attachments

  • LogFile_PS_AdBlock_V1.5.ps1.log
    1.9 KB · Views: 34

mrTGF

Member
Mar 23, 2015
40
19
UPDATE: I have done some digging and discovered that the first line of the "hosts" file has to be "127.0.0.1 localhost" for this to work, also I used the host sources in your created hosts file and added more from other websites and made this one, it seems to work for me on windows 10/Microsoft edge, have not yet tried on other platforms.
Instructions:
1. Download the file hosts.txt below
2 Copy hosts.txt in the C:/Windows/system32/drivers/etc
3. Backup your current hosts file (ie. copy and paste it to somewhere safe. (Desktop?)
4. Delete the hosts your original hosts file in the C:/Windows/system32/drivers/etc
5. Rename the hosts.txt to hosts

credit goes to: @Makorus and http://winhelp2002.mvps.org/

UPDATE NOTE: I'm assuming that some of the hosts files from Adaway that Makorus used in his hosts file seem to be android specific, the reason I'm mentioning this is because earlier on in the project I created a file only using those sources the result being that the ads would load after the page itself loaded. However when I also added the hosts from the above website it seemed to have worked perfectly, more testing would need to be done, but it seems to work
 

Attachments

  • hosts.txt
    506.4 KB · Views: 142
Last edited:

Makorus

Senior Member
Oct 4, 2012
195
171
github.com
Hello everyone

I just released a new build, version 1.5.1.
From now on, I'll use 3 numbers for a better versioning. Creating a version 1.6 for a tiny bug fix from version 1.5 would be a bit too heavy. :p
@mrTGF to be honest, I'm unable to reproduce your issue on my Windows 10 device, no issues on my Windows 10 device at all. Would you be so kind, download the new version 1.5.1 and give it a try?
I just realized your log file isn't complete. Did you cut the last few lines of the log files on your own? If not, the script has been terminated unexpectedly. Here's an example of a "complete" log file.

Code:
***************************************************************************************************
Started processing at [08/11/2015 22:16:36].
***************************************************************************************************

Running script version 1.5.1.

***************************************************************************************************

Checking for existing hostslist.ini file...
Didn't find existing hostslist.ini file - using default list...
Checking for existing whitelist.ini file...
Didn't find existing whitelist.ini file - using default list...
Checking whether the 'C:\Users\Makorus\Desktop\DL_Cache\' folder exists...
Download cache folder not found. Creating empty download cache folder...
Successfully created the download cache folder!
Starting download process for 'hosts' sources...
Downloading hosts file from: http://hostsfile.org/Downloads/hosts.txt
Downloading hosts file to: C:\Users\Makorus\Desktop\DL_Cache\1_source-hosts.txt
Downloading hosts file from: http://someonewhocares.org/hosts/zero/hosts
Downloading hosts file to: C:\Users\Makorus\Desktop\DL_Cache\2_source-hosts.txt
Downloading hosts file from: http://winhelp2002.mvps.org/hosts.txt
Downloading hosts file to: C:\Users\Makorus\Desktop\DL_Cache\3_source-hosts.txt
Downloading hosts file from: http://adaway.org/hosts.txt
Downloading hosts file to: C:\Users\Makorus\Desktop\DL_Cache\4_source-hosts.txt
Downloading hosts file from: http://pgl.yoyo.org/as/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext
Downloading hosts file to: C:\Users\Makorus\Desktop\DL_Cache\5_source-hosts.txt
Downloading hosts file from: http://www.malwaredomainlist.com/hostslist/hosts.txt
Downloading hosts file to: C:\Users\Makorus\Desktop\DL_Cache\6_source-hosts.txt
Finished download process!
Merging all hosts files into one big file, ignoring comment lines and duplicates, sorting and trimming lines...
Applying patch table...
Applying patch for '\t'...
Applying patch for ' {2,}'...
Applying patch for '0.0.0.0'...
Applying patch for 'hostsfile.org'...
Applying patch for 'someonewhocares.org'...
Applying patch for 'winhelp2002.mvps.org'...
Applying patch for 'adaway.org'...
Applying patch for 'pgl.yoyo.org'...
Applying patch for 'malwaredomainlist.com'...
Applying patch for 'bit.ly'...
Applying patch for 'spotify.com'...
Successfully finished apply process!
Creating new 'hosts' file, ready to be used...
Successfully created the 'hosts' file!
DNS Cache service is already disabled. It's safe to apply your new hosts file now!
Finished the PowerShell AdBlock script!
Elapsed time: 19.7985844 seconds

***************************************************************************************************
Finished processing at [08/11/2015 22:16:55].
***************************************************************************************************
 

mrTGF

Member
Mar 23, 2015
40
19
I did not make any changes to the file, that is exact output I received from the process, If you look at my last post you will notice that I resolved the issue by formatting the hosts file differently and adding more sources.

Hello everyone

I just released a new build, version 1.5.1.
From now on, I'll use 3 numbers for a better versioning. Creating a version 1.6 for a tiny bug fix from version 1.5 would be a bit too heavy. :p

@mrTGF to be honest, I'm unable to reproduce your issue on my Windows 10 device, no issues on my Windows 10 device at all. Would you be so kind, download the new version 1.5.1 and give it a try?
I just realized your log file isn't complete. Did you cut the last few lines of the log files on your own? If not, the script has been terminated unexpectedly. Here's an example of a "complete" log file.

Code:
***************************************************************************************************
Started processing at [08/11/2015 22:16:36].
***************************************************************************************************

Running script version 1.5.1.

***************************************************************************************************

Checking for existing hostslist.ini file...
Didn't find existing hostslist.ini file - using default list...
Checking for existing whitelist.ini file...
Didn't find existing whitelist.ini file - using default list...
Checking whether the 'C:\Users\Makorus\Desktop\DL_Cache\' folder exists...
Download cache folder not found. Creating empty download cache folder...
Successfully created the download cache folder!
Starting download process for 'hosts' sources...
Downloading hosts file from: http://hostsfile.org/Downloads/hosts.txt
Downloading hosts file to: C:\Users\Makorus\Desktop\DL_Cache\1_source-hosts.txt
Downloading hosts file from: http://someonewhocares.org/hosts/zero/hosts
Downloading hosts file to: C:\Users\Makorus\Desktop\DL_Cache\2_source-hosts.txt
Downloading hosts file from: http://winhelp2002.mvps.org/hosts.txt
Downloading hosts file to: C:\Users\Makorus\Desktop\DL_Cache\3_source-hosts.txt
Downloading hosts file from: http://adaway.org/hosts.txt
Downloading hosts file to: C:\Users\Makorus\Desktop\DL_Cache\4_source-hosts.txt
Downloading hosts file from: http://pgl.yoyo.org/as/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext
Downloading hosts file to: C:\Users\Makorus\Desktop\DL_Cache\5_source-hosts.txt
Downloading hosts file from: http://www.malwaredomainlist.com/hostslist/hosts.txt
Downloading hosts file to: C:\Users\Makorus\Desktop\DL_Cache\6_source-hosts.txt
Finished download process!
Merging all hosts files into one big file, ignoring comment lines and duplicates, sorting and trimming lines...
Applying patch table...
Applying patch for '\t'...
Applying patch for ' {2,}'...
Applying patch for '0.0.0.0'...
Applying patch for 'hostsfile.org'...
Applying patch for 'someonewhocares.org'...
Applying patch for 'winhelp2002.mvps.org'...
Applying patch for 'adaway.org'...
Applying patch for 'pgl.yoyo.org'...
Applying patch for 'malwaredomainlist.com'...
Applying patch for 'bit.ly'...
Applying patch for 'spotify.com'...
Successfully finished apply process!
Creating new 'hosts' file, ready to be used...
Successfully created the 'hosts' file!
DNS Cache service is already disabled. It's safe to apply your new hosts file now!
Finished the PowerShell AdBlock script!
Elapsed time: 19.7985844 seconds

***************************************************************************************************
Finished processing at [08/11/2015 22:16:55].
***************************************************************************************************
 

Top Liked Posts

  • There are no posts matching your filters.
  • 50
    Introduction
    Dear community

    This is my very first XDA: DevDB project. I'm glad to announce my ad-block solution purely based on PowerShell.
    I've been (and I still am) using AdAway on my Android phone. It's a great app, and it inspired me to get a similar solution for Windows computers.

    Version 0.1 was not public because it was slow as hell. It took about 8 minutes to handle ~200'000 hosts.
    With version 1.0, my script got a massive speed improvement. Now its duration has been greatly decreased. I achieved the same amount of hosts in less than 100 seconds now. But of course, these measured values shouldn't mean a lot, as they depend completely on your hardware (and a little bit on your internet connection of course).

    Now version 1.6 has an automatic backup & apply routine, check it out!

    Features
    This script does exactly what you think: It grabs various "hosts" sources from the internet, saves them locally, and merges them to one big file. Enjoy an ad-free Windows experience.

    • Download as many "hosts" files as you want
    • Merge them automatically into one big "hosts" file
    • Remove duplicates from the "hosts" file
    • Remove any comment line starting with # to keep your hosts file as small as possible
    • Apply a custom whitelist or even custom blacklists
    • Log the entire process to a log file (placed within the same directory where you saved this script)

    Downloads & Changelogs

    FAQs

    Disclaimer
    Altough this is normally a safe process, I am not responsible for possible damage to your device if anything should fail.
    I will not take any responsability for bricked Windows installations, other software defects or even lost data.

    License/forks
    This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License( ).

    This is an open source project, it's a PowerShell script and you're able to see the source code of course. You're allowed to modify this script as you want, as long as you give credits to me. Adaptations of my work shall be shared as well under the same license. Commercial use is allowed, as long as you don't sell my work.

    Requirements
    There are some requirements to check.
    • A little bit of PowerShell-knowledge (not necessary of course)
    • At least Windows 7 (theoretically it should work on any Windows)
    • At least PowerShell 3.0 (PowerShell 2.0 may work, but is unsupported)
    • At least 50 MB free space for this script
    • Administrative privileges - required if you want to use the automatic backup & apply routines, not for the script itself, otherwise you'll need to manually copy the "hosts" file into the Windows directory

    Instructions for use with administrator privileges (recommended)
    1. Download the archive and unzip it anywhere
    2. Execute the "_Run_Admin.bat" script via double-click, confirm the UAC window by clicking on the yes button and wait for the console to close itself
    3. Reboot your machine and enjoy an ad-free Windows experience
    4. Note: Instead of the batch script you can use the .ps1 file as well (unblock it first, simply right-click the .ps1 file, choose "Properties", then hit the "Unblock" button), start a new PowerShell console as administrator and manually run the script

    Instructions for use without administrator privileges
    1. Download the archive and unzip it anywhere
    2. Execute the "_Run_User.bat" script via double-click and wait for the console to close itself
    3. Copy the newly created "hosts" file and rename the existing "hosts" file to manually create a backup
      Code:
      C:/Windows/System32/drivers/etc/hosts
    4. Reboot your machine and enjoy an ad-free Windows experience
    5. Note: Instead of the batch script you can use the .ps1 file as well (unblock it first, simply right-click the .ps1 file, choose "Properties", then hit the "Unblock" button), start a new PowerShell console and manually run the script

    Instructions to customize psAdBlock
    1. To modify the "hosts" sources, simply edit the file "hostslist.ini"
    2. To allow specific hosts, simply edit the file "whitelist.ini"
    3. To block specific hosts, simply create a new .txt file within the "DL_Cache" folder and add the hosts (one per line) you want to block (e.g. "0.0.0.0 place-any-site-here.com")

    Credits & thanks to
    * Makorus: Creator of this PowerShell script
    * Dominik Schuermann: Inspiring me to do this
    * Luca Sturlese: Allowing us to use his awesome logging function library

    Any questions or suggestions? Did you find any bugs? Thanks for your feedbacks in advance! :good:

    XDA:DevDB Information
    psAdBlock, Tool/Utility for the Windows 8 General

    Contributors
    Makorus
    Source Code: https://github.com/makorus/ps-adblock


    Version Information
    Status: Stable
    Current Stable Version: 1.6.5
    Stable Release Date: 2017-07-03

    Created 2015-05-09
    Last Updated 2018-07-13
    41
    News
    I'm moving this project to GitHub for many reasons. Future releases will be available through GitHub only.

    New download link:
    https://github.com/makorus/PS-AdBlock/archive/master.zip


    Current release & changelog
    Code:
    ##-------------------------------------------------------------------------------------##
    ## V1.6.3, 2018-02-03                                                                  ##
    ## - Added logic to handle 'localhost' in whitelist (thanks to antonio-gil)            ##
    ## - Removed script version from filename                                              ##
    ##-------------------------------------------------------------------------------------##

    Notes
    Code:
    #########################################################################################
    ## Notes:                                                                              ##
    ## - PowerShell 3.0 or newer is recommended to run this script without any errors      ##
    ## - PowerShell 2.0 may work as well, but is unsupported                               ##
    ## - If you disable the backup/apply mode you have to manually copy the final          ##
    ##   'hosts'-file to the Windows etc directory (please manually backup the old file:   ##
    ##   C:\Windows\System32\drivers\etc                                                   ##
    ## - To apply a custom blacklist, simply create a text file containing your desired    ##
    ##   hosts and save it in the download cache folder 'DL_Cache' (run the script once)   ##
    ## - To manually add your custom hosts list, create a new text file called             ##
    ##   'hostslist.ini' and add your hosts sources (one site per line without the www)    ##
    ## - To automatically remove specific sites from the hosts list, create a new text     ##
    ##   file called 'whitelist.ini" and add your sites you want to whitelist              ##
    ## - If you experience performance issues like extremely slow Windows startup time,    ##
    ##   Try to disable the client DNS Cache service via CMD:                              ##
    ##     sc config dnscache start= disabled                                              ##
    #########################################################################################

    Release history & changelogs
    Code:
    #########################################################################################
    ## Release history (yyyy-MM-dd):                                                       ##
    ##-------------------------------------------------------------------------------------##
    ## V0.0.1 - V0.0.9, 2015-05-02                                                         ##
    ## - Non-public test versions only                                                     ##
    ##-------------------------------------------------------------------------------------##
    ## V1.0.0, 2015-05-09                                                                  ##
    ## - First public and stable release                                                   ##
    ##-------------------------------------------------------------------------------------##
    ## V1.1.0, 2015-05-10                                                                  ##
    ## - Added release history to the script                                               ##
    ## - Merged Log Functions to the main script file (no more two separate files)         ##
    ## - Added XDA:DevDB link                                                              ##
    ## - Added script version to name (easy identification)                                ##
    ##-------------------------------------------------------------------------------------##
    ## V1.2.0, 2015-05-13                                                                  ##
    ## - Fixed issue with old 'New-Item' command in function 'Log-Start':                  ##
    ## --> OLD: New-Item -Path $LogPath -Value $LogName -ItemType File                     ##
    ## --> NEW: New-Item -Path "$sFullPath" -ItemType File                                 ##
    ## - Changed the 'Log-Start' invoking command to use $PSScriptRoot:                    ##
    ## --> OLD: Log-Start -LogPath ".\" [...]                                              ##
    ## --> NEW: Log-Start -LogPath "$PSScriptRoot" [...]                                   ##
    ## - Added #region and #endregion codes to fold the code                               ##
    ##-------------------------------------------------------------------------------------##
    ## V1.3.0, 2015-05-13                                                                  ##
    ## - Completely reworked the 'release history', huge improvement to readability        ##
    ## - The 'Patch Table' uses now '-ireplace' instead '-replace' for better results      ##
    ## - Changed the 'Patch Table' to use regex expressions for my default list            ##
    ## - Added Spotify to 'Patch Table':                                                   ##
    ## --> $DefaultPatchTable.Add('\w+.spotify.com', 'localhost')                          ##
    ##-------------------------------------------------------------------------------------##
    ## V1.4.0, 2015-07-26                                                                  ##
    ## - Reworked whitelist method (check notes for more information)                      ##
    ## - Reworked hosts source list (check notes for more information)                     ##
    ## - Reworked the 'Patch Table', doesn't require regex expressions anymore             ##
    ##-------------------------------------------------------------------------------------##
    ## V1.5.0, 2015-08-10                                                                  ##
    ## - The file now checks for the client DNS Cache service and logs a warning if it's   ##
    ##   enabled to ensure you don't run into troubles                                     ##
    ## - Removed the note about the DNS Cache service: It's not recommended to set the     ##
    ##   startup type to "demand" (manual), it will cause issues sooner or later           ##
    ## - Starting from now on, I'll create an executable file (to be found in the ZIP)     ##
    ## - I'll also attach a VirusTotal.com scan to every new release                       ##
    ## --> Please report any Anti-Virus false positive alarms to me                        ##
    ##-------------------------------------------------------------------------------------##
    ## V1.5.1, 2015-08-11                                                                  ##
    ## - Fixed issue with wrong $PSScriptRoot variable when using the EXE variant          ##
    ## - Changed the 'release history' to allow better versioning (e.g. version 1.5.1)     ##
    ## --> Note: Older versions still contain two numbers only (e.g. version 1.2 etc.)     ##
    ##-------------------------------------------------------------------------------------##
    ## V1.5.2, 2015-09-13                                                                  ##
    ## - Remove every empty line in the hosts file                                         ##
    ## - Changed the Log-Write function to use Write-Host instead of Write-Debug, now      ##
    ##   the entire log will be shown during the script execution (live)                   ##
    ## - Changed replacement method to insert 0.0.0.0 instead of 127.0.0.1 in the final    ##
    ##   hosts file which leads to a much better and faster DNS resolution                 ##
    ## - Future releases will be delivered with additional hosts files, for example to     ##
    ##   block Google ad servers and some Windows 10 hosts to stop this privacy nightmare  ##
    ##-------------------------------------------------------------------------------------##
    ## V1.5.3, 2015-10-08                                                                  ##
    ## - Fixed ugly encoding issue, new 'hosts' file will be created correctly now, means  ##
    ##   no BOM (Byte Order Mark) and no UCS-2 encoding anymore, should fix many issues    ##
    ##-------------------------------------------------------------------------------------##
    ## V1.5.4, 2015-10-09                                                                  ##
    ## - Fixed duplicate hostname issue, cmdlet Get-Unique allows by default hostnames     ##
    ##   with capital letters (Get-Unique is a case sensitive search)                      ##
    ##   e.g. 'test.com' and 'Test.com' are being handled correctly now                    ##
    ## - Reverted warning about DNS cache service, there should be no issue with the DNS   ##
    ##   cache service because the 'hosts' file is being encoded as a typical UTF8 file    ##
    ## - Added new debug mode, set $DebugMode to $true to activate it                      ##
    ##   This skips the download process, useful if you want to test something without     ##
    ##   re-downloading the whole host source lists                                        ##
    ##-------------------------------------------------------------------------------------##
    ## V1.5.5, 2015-10-10                                                                  ##
    ## - Fixed issue with method ToLower (should work with PowerShell 2.0 now), ToLower    ##
    ##   is a method to work with strings only, work around with a ForEach-Object cmdlet   ##
    ## - Added a PowerShell version check, unsupported versions are PowerShell v1 and v2,  ##
    ##   if you start this script with an unsupported PowerShell version, you'll find      ##
    ##   some hints in the log file to inform you about that                               ##
    ##-------------------------------------------------------------------------------------##
    ## V1.5.6, 2015-10-11                                                                  ##
    ## - Added again regex patterns to patch table, whitelist should work now correctly    ##
    ## - Debug mode now skips the patch process as well                                    ##
    ##-------------------------------------------------------------------------------------##
    ## V1.5.7, 2015-10-17                                                                  ##
    ## - Completely replaced previous regex patterns with one more complex pattern to      ##
    ##   handle hostnames with subdomains, performance increased significantly             ##
    ##-------------------------------------------------------------------------------------##
    ## V1.5.8, 2016-01-03                                                                  ##
    ## - Non-public test version only (skipped)                                            ##
    ##-------------------------------------------------------------------------------------##
    ## V1.5.9, 2016-01-24                                                                  ##
    ## - Non-public test version only (skipped)                                            ##
    ##-------------------------------------------------------------------------------------##
    ## V1.6.0, 2016-02-29                                                                  ##
    ## - Added function to check for admin privileges                                      ##
    ## - Added routine for automatic apply of 'hosts'-file (admin privileges required)     ##
    ## - Added routine for backup of 'hosts'-file (admin privileges required)              ##
    ## - Added some Skype and YouTube hostnames to the whitelist for proper functionality  ##
    ## - Added new icon for the executable file                                            ##
    ## - Updated the notes (see below)                                                     ##
    ## - Overall cleanup, added region codes for the main script                           ##
    ##-------------------------------------------------------------------------------------##
    ## V1.6.1, 2016-04-19                                                                  ##
    ## - Fixed wrong script number                                                         ##
    ##-------------------------------------------------------------------------------------##
    ## V1.6.2, 2017-04-25                                                                  ##
    ## - Added Goo.gl to 'Patch Table' and to the default whitelist.ini:                   ##
    ## --> $DefaultPatchTable.Add('\w+.goo.gl', 'localhost')                               ##
    ## - Changed the foreach loop to avoid recreating the object System.Net.WebClient      ##
    ## - Added error handling for the download process                                     ##
    ## - Added routine to correctly check the existence of the original 'hosts'-file       ##
    ## - Removed EXE file because of false positive alarms from many AntiVirus engines     ##
    ## - Added instead two new batch files: '_Run_Admin.bat' and '_Run_User.bat'           ##
    ## - Overall rework, changes many comments and updated some notes                      ##
    ##-------------------------------------------------------------------------------------##
    9
    FAQs
    • How to execute this PowerShell script?
      Option 1: Run the script using one of the included batch scripts, either the "_Run_Admin.bat" or the "_Run_User.bat" batch script.
      Option 2: Manually run the PowerShell script. But at first, you have to unblock the PowerShell script because it is a script you downloaded from the internet. Once you unzipped the file, do a right click on the PS1 file and open its file properties. Click on the "Unblock" button and you're done. Right click the file and click on "Run with PowerShell".
    • Why do I get an message warning me about an execution policy?
      Microsoft implemented this security feature when they published PowerShell. By default, the execution policy is set to "restricted", which means no scripts can be executed directly. You're able to configure the execution policy as you want. There are several execution policies, you can check them out here: Microsoft TechNet, Set-ExecutionPolicy.
      I personally recommend to change the execution policy to "unrestricted", this means you can execute scripts you created and even those you downloaded from the internet (though you have to be careful what you download and what you execute).
    • How to uninstall this PowerShell script?
      Basically, you just have to navigate to this folder:
      C:\Windows\System32\drivers\etc
      Delete the new hosts file and restore the original one by renaming "hosts.original" to "hosts" without any extension. After that, simply delete the files you downloaded from this thread.
    4
    A late happy new year to everyone! :)

    I just released version 1.6.3 with a few small changes.

    I'm gradually moving this project completely to GitHub, therefore there's no need for an additional VirusTotal scan. Downloads will be moved to GitHub from now on. Contribution to this project is now even easier. :)
    3
    Thanks for this great tool. Works great but can someone make an .exe out of it? It would be more convenient.


    Hi there, thank you for your reply. I can compile an EXE file out of it, but I expect a lot of false reports from anti-virus applications. I'll upload the EXE file tomorrow.

    Kind regards
    Makorus