Summary: This wiki page shows how I added google ads to each wiki page in a Repository using powershell, and use a VS Code extension to make sure new files get the same treatment.
Date: 25 October 2025
When using Google Auto Adsense on my wiki, I get ads like everywhere, which makes the pages look cluttered and hard to read. My solution is to add an ad at the bottom of each page. However, I don not want to do that manually.
I originally wanted to use git hooks to run a script. However, git hooks have a notourious bad reputation when running them on Windows. So instead I opted to use a VS Code extension that runs a script when saving a file.
I previously used this Run on Save extension for work, which worked very well, so I decided to use it here again.
After installing the extension, you need to add some configuration to your settings.json file. I've used the vscode settings extensively before, so I just needed to add the following settings:
"emeraldwalk.runonsave": {
// Messages to show before & after all commands
"message": "*** All Start ***",
"messageAfter": "*** All Complete ***",
// Show elappsed time for all commands
"showElapsed": true,
"commands": [
{
"match": "\\.txt$",
"notMatch": "\\drafts/.*$",
"cmd": "pwsh.exe -ExecutionPolicy Bypass -File ${workspaceFolder}\\ads\\Add-AdsToWikiPages.ps1"
}
]
}
This will run the powershell script located in the ads folder every time a .txt file is saved in the dokuwiki/pages folder.
This is the powershell script me and copilot created together and I tested very thoroughly before using it on my actual wiki pages:
function Add-StringToFiles { param( [Parameter(Mandatory)] [string]$FolderPath, [Parameter(Mandatory)] [string]$StringToAdd, [string[]]$ExcludeList = @() ) Get-ChildItem -Path $FolderPath -File -Filter "*.txt" | ForEach-Object { $filePath = $_.FullName $fileName = $_.Name # Skip if file is in exclude list if ($ExcludeList -contains $fileName) { Write-Host "Skipping excluded file: $fileName" return } $content = Get-Content -Path $filePath -Raw if ($content -notmatch [regex]::Escape("//This wiki has been made possible by://")) { $newContent = "$content`r`n$StringToAdd`r`n" Set-Content -Path $filePath -Value $newContent } } } $folder = "C:\Repos\GetShifting\knowledge\dokuwiki\pages" $excludeFiles = @("overview.txt", "alltags.txt", "sidebar.txt") $adString = '//This wiki has been made possible by:// <HTML> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8613096447910897" crossorigin="anonymous"></script> <!-- Wiki End of Page --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-8613096447910897" data-ad-slot="6221699236" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </HTML> ' Add-StringToFiles -FolderPath $folder -StringToAdd $adString -ExcludeList $excludeFiles
This wiki has been made possible by: