Thursday, 6 August 2026

Reusable Template Script for Any Windows Server-Ransomware-Protection


# ==============================================================================

# Script: Configure-TallyRansomware-Selvamani.ps1

# Target Data Path: E:\TallyPrimeSelvamani\data

# Target Executable: E:\TallyPrimeSelvamani\tally.exe

# ==============================================================================


# 1. Define Exact Paths

$TallyDataPath = "E:\TallyPrimeSelvamani\data"

$TallyExePath  = "E:\TallyPrimeSelvamani\tally.exe"


$ErrorActionPreference = "Stop"


Write-Host "====================================================" -ForegroundColor Cyan

Write-Host " Configuring Protection & Performance for Tally     " -ForegroundColor Cyan

Write-Host "====================================================" -ForegroundColor Cyan


# 2. Add Data Folder to Ransomware Shield (Protected Folders)

if (Test-Path -Path $TallyDataPath) {

    Write-Host "[+] Adding Protected Data Folder: $TallyDataPath" -ForegroundColor Yellow

    Add-MpPreference -ControlledFolderAccessProtectedFolders $TallyDataPath

    Write-Host "    Successfully added protected folder." -ForegroundColor Green

} else {

    Write-Host "[!] Warning: Folder '$TallyDataPath' not found. Please verify directory name." -ForegroundColor Red

}


# 3. Whitelist tally.exe (Allow Application through CFA)

if (Test-Path -Path $TallyExePath) {

    Write-Host "`n[+] Whitelisting Executable: $TallyExePath" -ForegroundColor Yellow

    Add-MpPreference -ControlledFolderAccessAllowedApplications $TallyExePath

    Write-Host "    Allowed executable successfully." -ForegroundColor Green

} else {

    Write-Host "[!] Warning: Executable '$TallyExePath' not found. If installed elsewhere, adjust path." -ForegroundColor Red

}


# 4. Add Defender Performance Exclusions (Prevents network scanning lag)

Write-Host "`n[+] Adding Performance Exclusions..." -ForegroundColor Yellow

Add-MpPreference -ExclusionProcess "tally.exe"

if (Test-Path -Path $TallyDataPath) {

    Add-MpPreference -ExclusionPath $TallyDataPath

}

Add-MpPreference -ExclusionExtension ".tsf"

Add-MpPreference -ExclusionExtension ".900"

Write-Host "    Performance exclusions configured successfully." -ForegroundColor Green


# 5. Enable Controlled Folder Access (Audit Mode First)

# Options: "AuditMode" (Safe testing) or "Enabled" (Immediate enforcement)

$DesiredMode = "AuditMode" 


Write-Host "`n[+] Setting Controlled Folder Access Mode to: $DesiredMode" -ForegroundColor Yellow

Set-MpPreference -EnableControlledFolderAccess $DesiredMode

Write-Host "    CFA mode updated." -ForegroundColor Green


# 6. Verify Settings

Write-Host "`n====================================================" -ForegroundColor Cyan

Write-Host " Current Controlled Folder Access Summary           " -ForegroundColor Cyan

Write-Host "====================================================" -ForegroundColor Cyan


$mpPrefs = Get-MpPreference


Write-Host "CFA Mode              : " -NoNewline

switch ($mpPrefs.EnableControlledFolderAccess) {

    0 { Write-Host "Disabled (0)" -ForegroundColor Red }

    1 { Write-Host "Enabled / Active Blocking (1)" -ForegroundColor Green }

    2 { Write-Host "Audit Mode Only (2)" -ForegroundColor Yellow }

}


Write-Host "`nProtected Folders:" -ForegroundColor Cyan

$mpPrefs.ControlledFolderAccessProtectedFolders | Where-Object { $_ -like "*Selvamani*" } | ForEach-Object { Write-Host " - $_" -ForegroundColor White }


Write-Host "`nAllowed Applications:" -ForegroundColor Cyan

$mpPrefs.ControlledFolderAccessAllowedApplications | Where-Object { $_ -like "*tally*" } | ForEach-Object { Write-Host " - $_" -ForegroundColor White }


Write-Host "`nConfiguration complete!" -ForegroundColor Green