You can export a list of files larger than 100MB modified after January 1, 2019, using Windows PowerShell within a batch script. The script searches specified directories, filters by size and date, and outputs the results to either a CSV or TXT file for easy review.
Understanding the Requirements
Before writing the script, it is essential to define the exact criteria for the file search. You need to identify files that meet two specific conditions simultaneously: their size must exceed 100 megabytes (MB), and their last modification date must be later than January 1, 2019. This combination helps isolate large, recently active data files while ignoring older archives or small temporary files. The output format is also a critical decision point. A CSV file is generally preferred for further analysis because it can be opened in Excel or imported into databases with proper column separation. However, a plain text file might be sufficient if you only need a quick visual check. Both formats require careful handling of file paths, especially those containing spaces or special characters, to ensure the data remains valid.Creating the Batch Script Structure
A batch file acts as a wrapper that calls PowerShell commands. This approach is robust because PowerShell has superior object-oriented filtering capabilities compared to traditional command-line tools likedir or forfiles. Start by creating a new text file and renaming it with a .bat extension. Inside this file, you will invoke PowerShell with the -Command parameter.
It is important to handle paths correctly. If your target directory contains spaces, you must enclose the path in quotes. For more complex scenarios involving nested folders, consider reading about how to properly handle spaces in batch files to avoid common parsing errors. You can refer to Batch File Not Working with Space in File Name for detailed troubleshooting on quote escaping and variable expansion.
Here is the basic structure of the batch file:
@echo off
setlocal enabledelayedexpansion
:: Define variables
set "TargetPath=C:\Your\Target\Folder"
set "OutputFile=C:\Output\LargeFiles.csv"
set "MinSizeBytes=104857600"
set "StartDate=01/01/2019"
powershell -Command ^
"& {
# PowerShell code goes here
}"
In this snippet, the variables are defined at the top for easy modification. The MinSizeBytes variable represents 100MB in bytes, which is necessary because PowerShell's Get-ChildItem cmdlet returns file sizes in bytes. The StartDate variable sets the cutoff date for the modification time filter.
Implementing the PowerShell Logic
Inside the PowerShell block, you will use theGet-ChildItem cmdlet (aliased as gci) to retrieve files recursively. You must apply the -Recurse flag to search all subdirectories. Then, pipe the results to Where-Object to apply your filters.
The first filter checks the length of the file. Since Length is in bytes, you compare it against 104857600 (100 * 1024 * 1024). The second filter compares the LastWriteTime property against the start date. Note that PowerShell handles date strings differently depending on locale settings, so it is often safer to create a [datetime] object explicitly.
Get-ChildItem -Path $TargetPath -Recurse -File | Where-Object {
$_.Length -gt 104857600 -and
$_.LastWriteTime -gt [datetime]'2019-01-01'
} | Select-Object FullName, Length, LastWriteTime | Export-Csv -Path $OutputFile -NoTypeInformation
This code pipeline retrieves files, filters them, selects relevant properties (Name, Size, Date), and exports them to CSV. The -NoTypeInformation flag removes the type header from the CSV, making it cleaner. If you prefer a text file, replace Export-Csv with Out-File and adjust the selected properties accordingly.
Handling Errors and Performance
Searching large drives can take significant time and may encounter permission errors. To prevent the script from stopping on access denied errors, you can add the-ErrorAction SilentlyContinue parameter to Get-ChildItem. This ensures that inaccessible folders are skipped rather than halting the entire process.
Additionally, consider the scope of the search. Searching the entire C: drive is inefficient and often unnecessary. It is better to specify known directories where large files are likely stored, such as C:\Users, C:\ProgramData, or specific project folders. If you need to run this regularly, look into Scheduling a Task to Start a Batch File with Windows Task Scheduler to automate the execution without manual intervention.
Finally, verify the output file location exists before running the script. If the directory for $OutputFile does not exist, PowerShell will throw an error. You can add a simple check in the batch file to create the directory if needed, ensuring a smooth execution every time.
Frequently Asked Questions
Can I change the size threshold to 50MB?
Yes, simply update the MinSizeBytes variable to 52428800 (50 * 1024 * 1024) in the batch script. The rest of the logic remains unchanged as it dynamically uses this variable for comparison.
What if my files have spaces in their names?
The PowerShell Export-Csv cmdlet automatically handles quotes around fields containing commas or spaces. However, ensure your batch file variables are properly quoted when passed to PowerShell to avoid parsing issues.
How do I exclude hidden files from the search?
By default, Get-ChildItem does not return hidden files unless you specify the -Force parameter. Therefore, the provided script already excludes hidden files, focusing only on visible items.
Can I export the list to a JSON file instead?
Yes, replace Export-Csv with ConvertTo-Json and redirect the output to a file using Out-File. This provides a structured format suitable for API integrations or modern data processing tools.
Why is the script running slowly?
Recursive searches on large drives are inherently slow due to disk I/O overhead. Limiting the -Path to specific directories and avoiding system folders like Windows or Program Files can significantly improve performance.