Ever needed to split a large list into smaller files? Here's a simple PowerShell script that does exactly that.
The Problem
You have a text file with thousands of domains, and you need to break it into smaller chunks. Maybe for transport rules, parallel processing, or just to make it more manageable.
The Solution
This script takes your big domain list and splits it into equal parts:
# Full path to the original domain list
$inputFile = "domains.txt"
# Output folder (must exist)
$outputFolder = "C:\temp\SplitOutput"
# Read all domains (trim whitespace, ignore empty lines)
$domains = Get-Content $inputFile | Where-Object { $_.Trim() -ne "" }
# Number of files to split into
$splitCount = 4
# Calculate number of lines per file
$perFile = [Math]::Ceiling($domains.Count / $splitCount)
# Split and write to files
for ($i = 0; $i -lt $splitCount; $i++) {
$startIndex = $i * $perFile
$chunk = $domains[$startIndex..([Math]::Min($startIndex + $perFile - 1, $domains.Count - 1))]
$outputFile = Join-Path $outputFolder "senders_part$($i + 1).txt"
$chunk | Set-Content $outputFile
Write-Host "Created $outputFile with $($chunk.Count) domains"
}
What It Does
- Reads your domain list file
- Removes empty lines and extra spaces
- Calculates how many domains per file
- Creates the split files with names like
senders_part1.txt
,senders_part2.txt
, etc. - Shows you what it created
How to Use It
- Put your domain list in domains.txt
- Make sure the output folder exists
- Change $splitCount if you want more or fewer files
- Run the script
That's it. Your big list is now split into manageable chunks, ready to use however you need them.