PowerShell Script: Create Multiple Test CSV Files

In this post we share a PowerShell script that generates multiple CSV files in the current directory, each containing a random number of rows with sample data. Please note that this script uses basic random data for illustration purposes, and you may customize it based on your specific requirements.

PowerShell Script to Generate CSV Files

# Define the number of CSV files to create
$numberOfFiles = 5

# Define the range of rows for each CSV file
$minRows = 10
$maxRows = 50

# Loop to create multiple CSV files
for ($i = 1; $i -le $numberOfFiles; $i++) {
    # Generate a random number of rows for each file
    $rowCount = Get-Random -Minimum $minRows -Maximum $maxRows

    # Generate sample data with random values
    $data = 1..$rowCount | ForEach-Object {
            ID = $_
            Name = "User$($_)"
            Age = Get-Random -Minimum 20 -Maximum 50
            City = (Get-Random -InputObject @('New York', 'London', 'Tokyo'))

    # Create a CSV file with the generated data
    $filePath = "RandomData_$i.csv"
    $data | Export-Csv -Path $filePath -NoTypeInformation

    Write-Host "CSV file '$filePath' created with $rowCount rows."
PowerShell Create Test CSV Files

This script offers flexibility for creating a specified number of CSV files, each containing a random assortment of rows and diverse sample data. The provided script serves as a foundational template that can be tailored to meet the specific requirements of your testing or demonstration scenarios.

Stay tuned for more PowerShell insights and practical tips from DBASco.

Leave a Reply

Your email address will not be published. Required fields are marked *